Skip to main content

Django Model Admin Fields

Django is one of the most popular Python web frameworks today. Importantly, it provides an ORM permitting us to define models as Python classes that Django maps to a database representations for us.

A very common, nearly ubiquitous desire/need I have is to keep some record against all database objects as to who created them, when and who last edited them and when. Very basic tracking fields. Given the ubiquity of the need it was best implemented as an abstract model that my models derive from.

The basic Django example of model:

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

can be extended thusly:

from django.db import models
from django_model_admin_fields import AdminModel

class Person(AdminModel):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

which has the simple effect of adding the following fields to the model silently:

    created_by = models.ForeignKey(User, verbose_name='Created By', 
                                   related_name='%(class)ss_created', 
                                   editable=False, null=True, on_delete=models.SET_NULL)
    created_on = models.DateTimeField('Time of Creation', editable=False, null=True)
    created_on_tz = TimeZoneField('Time of Creation, Timezone', 
                                  default=settings.TIME_ZONE, editable=False)

    last_edited_by = models.ForeignKey(User, verbose_name='Last Edited By', 
                                       related_name='%(class)ss_last_edited', 
                                       editable=False, null=True, on_delete=models.SET_NULL)
    last_edited_on = models.DateTimeField('Time of Last Edit', editable=False, null=True)
    last_edited_on_tz = TimeZoneField('Time of Last Edit, Timezone', 
                                      default=settings.TIME_ZONE, editable=False)

(a more precise description of course is in __init__.py)

Importantly it also overrides the model's save() method to set those six fields before calling super().save() (i.e. the default save method) and thus these fields are automatically managed.

The currently active Django timezone is saved as well to support sensible human interpretation of the saved times (as Django's DateTimeField) is not timezone aware.

To make use of that easier, two properties are also added to the model: created_on_local and last_edited_on_local which are timezone aware versions of the naive created_one and last_edited_on fields.

To illustrate use of the Person example above:

person = Person()
person.first_name = "John"
person.last_name = "Smith"
person.save

print(f"{person.first_name} {person.last_name}")
print(f"was created by {person.created_by} on {person.created_on_local}.")

Of course to make use of local times, you need to activate the timezone that the creating user is in. To do that you need to know it first. The JavaScript library jstz is useful in that regard for detecting the users timezone and there's a great guide on setting timezones in Django in the Django documentation proper.

Metadata

Release files for django-model-admin-fields 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-model-admin-fields 0.3.1
File Size Uploaded
django_model_admin_fields-0.3.1.tar.gz 9.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-model-admin-fields 0.3.1
File Interpreter ABI Platform
django_model_admin_fields-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.6 kB

Release files / django_model_admin_fields-0.3.1.tar.gz

Download URL django_model_admin_fields-0.3.1.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a4d39ba4f9c6e58a0f097aafaddf1d08f03b2c3fa115a8d708b3f023dfbdecd6
BLAKE2b-256 checksum
How to use checksums
1f0f9b8a46a9735b38f975579607729c189e0e8850afca40eea23af7739515bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release files / django_model_admin_fields-0.3.1-py3-none-any.whl

Download URL django_model_admin_fields-0.3.1-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a5d1d5ee130e60adf47306f55040485c53a63f92b94ebb94d54a8fe0dc9c7c9
BLAKE2b-256 checksum
How to use checksums
c3813a6fa554f6190aa9101a5ab2f27fc16228aa35a4dd9f4774bd8581f9b9a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page