Ad

How Do I Make A Non-editable Field Editable In Django?

- 1 answer

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.

Ad

Answer

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)

Ad
source: stackoverflow.com
Ad