Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to show created and modified in admin #186

Closed
nemesifier opened this issue Sep 29, 2015 · 1 comment
Closed

Possibility to show created and modified in admin #186

nemesifier opened this issue Sep 29, 2015 · 1 comment

Comments

@nemesifier
Copy link

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:

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin import ModelAdmin

from model_utils.fields import AutoCreatedField, AutoLastModifiedField


class TimeStampedEditableModel(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)

    class Meta:
        abstract = True


class TimeStampedEditableAdmin(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.

@carljm
Copy link
Collaborator

carljm commented Sep 29, 2015

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.

@carljm carljm closed this as completed Sep 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants