Skip to main content

Custom list display of model timestamps for Django Admin.

Project description

Show human readable created/modified dates and/or times in the django.contrib.admin changelist for models that track the moment of creation and updates.

Usage

First you will need to have a model that tracks the moment of creation and modification. The barebones example for that is:

from django.db import models

class TimestampedItem(models.Model):
    created = models.DateTimeField('created', auto_now_add=True)
    modified = models.DateTimeField('modified', auto_now=True)

Or, if you are using django-extensions:

from django_extensions.db.models import TimeStampedModel

class TimestampedItem(TimeStampedModel):
    pass

Now it’s time to register this model with Django’s admin site.

Create a admin class, just like you’d usually do, but make it extend TimestampedAdminMixin as well as any other base admin class you use:

from django.contrib import admin
from models import TimestampedItem
from admintimestamps import TimestampedAdminMixin

class TimestampedAdmin(TimestampedAdminMixin, admin.ModelAdmin):
    pass

admin.site.register(TimestampedItem, TimestampedAdmin)

That’s it!

https://bitbucket.org/jaap3/django-admin-timestamps/raw/tip/img/screenshot.png

Configuration

If the names of you timestamp fields aren’t created and modified but for example created_at and modified_at you’ll need to do a tiny bit of configuration in the model admin class:

class TimestampedAdmin(TimestampedAdminMixin, admin.ModelAdmin):
    timestamp_fields = ('created_at', 'modified_at')

You can also use the timestamp_fields tuple to only show one field by only specifying the field you want to show:

class TimestampedAdmin(TimestampedAdminMixin, admin.ModelAdmin):
    timestamp_fields = ('modified',)

Changelog for django-admin-timestamps

1.1.1 (2013-07-01)

  • Handle None values properly

1.1 (2012-02-21)

  • Just displaying one timestamp field is now possible.

  • Proper handling of date fields.

1.0.1 (2011-10-11)

  • Fixed setup.py to include the compat subpackage

1.0.0 (2011-10-07)

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-admin-timestamps-1.1.1.zip (41.9 kB view details)

Uploaded Source

File details

Details for the file django-admin-timestamps-1.1.1.zip.

File metadata

File hashes

Hashes for django-admin-timestamps-1.1.1.zip
Algorithm Hash digest
SHA256 33e3dc5f039979bba39060eb22ec5439e97adfb0af8d9d3db286aeca3df837ff
MD5 659b33d4c84c9894b68cf88399627fc6
BLAKE2b-256 8315237d260f263a97c73952e324ef7008837025b95f9408b77303a12560e238

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page