You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to be able to show the fields "created" and "modified" in the admin form as readonly fields without too much boilerplate. I believe I'm not the only one doing this, maybe it would make sense to add it to this useful library.
This is the code I actually use:
fromdjango.dbimportmodelsfromdjango.utils.translationimportugettext_lazyas_fromdjango.contrib.adminimportModelAdminfrommodel_utils.fieldsimportAutoCreatedField, AutoLastModifiedFieldclassTimeStampedEditableModel(models.Model):
""" An abstract base class model that provides self-updating ``created`` and ``modified`` fields. """created=AutoCreatedField(_('created'), editable=True)
modified=AutoLastModifiedField(_('modified'), editable=True)
classMeta:
abstract=TrueclassTimeStampedEditableAdmin(ModelAdmin):
""" ModelAdmin for TimeStampedEditableModel """def__init__(self, *args, **kwargs):
self.readonly_fields+= ('created', 'modified',)
super(TimeStampedEditableAdmin, self).__init__(*args, **kwargs)
TimeStampedEditableAdmin automatically adds created and modified to the list of readonly fields at the bottom.
What do you think? If you like this idea I can send a PR.
The text was updated successfully, but these errors were encountered:
Thanks for the idea. I'm not too excited about adding stuff like this, it's not that much code to do it yourself, and there are too many different ways someone might want this to work.
Hi,
I would like to be able to show the fields "created" and "modified" in the admin form as readonly fields without too much boilerplate. I believe I'm not the only one doing this, maybe it would make sense to add it to this useful library.
This is the code I actually use:
TimeStampedEditableAdmin
automatically addscreated
andmodified
to the list of readonly fields at the bottom.What do you think? If you like this idea I can send a PR.
The text was updated successfully, but these errors were encountered: