Skip to main content

Django abstract model that adds admin fields (created_on/by, laste_edited_on/by) to an existing model

Project description

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.

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_model_admin_fields-0.3.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

django_model_admin_fields-0.3-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file django_model_admin_fields-0.3.tar.gz.

File metadata

  • Download URL: django_model_admin_fields-0.3.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

File hashes

Hashes for django_model_admin_fields-0.3.tar.gz
Algorithm Hash digest
SHA256 41a65955c55615a4653a2fdc47cd41a421445065fba1c2d41f21474f4a6b99fa
MD5 b7a01cb5676d973c759d59d61320d2bd
BLAKE2b-256 1d21f7556efe514dc99939cc94d7719d49ea5925673819300ad6a28f783d7f6e

See more details on using hashes here.

File details

Details for the file django_model_admin_fields-0.3-py3-none-any.whl.

File metadata

  • Download URL: django_model_admin_fields-0.3-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

File hashes

Hashes for django_model_admin_fields-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9cdbf5fdb75bb4ab86d797ac147cea312d5fd3457747a7da2caa54415adf62f8
MD5 4a0fa87e155ad8a07e83c14c06756dab
BLAKE2b-256 2e268e74ad4accc03fd2509236f9d45d867ce9a6239374ef36f9b6925034ad8c

See more details on using hashes here.

Supported by

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