Home > Net >  How do I make a non-editable field editable in Django?
How do I make a non-editable field editable in Django?

Time:02-11

I have a field creation with auto_now_add=True in my model. I want to be able to edit this from the admin website, but when I try to display the field, I get the following error:

'creation' cannot be specified for model form as it is a non-editable field

I tried adding editable=True to the model field as per the docs, but that did not work.

CodePudding user response:

The fields with auto_now_add=True are not editable. You need remove the auto_now_add, and set a default value or overwrite the model save method to set the creation date.

created = models.DateTimeField(default=timezone.now)

... or ...


class MyModel(models.Model):
    # ...

    def save(self, *args, **kw)
        if not self.created:
            self.created = timezone.now()
        super(MyModel, self).save(*args, **kw)

  • Related