mincom
mincom

Reputation: 979

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

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.

Upvotes: 0

Views: 1216

Answers (1)

Tonio
Tonio

Reputation: 1782

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)

Upvotes: 2

Related Questions