Skip to main content

Reusable abstract django models

Project description

gs-django-modelplus

Django application modelplus defines reusable abstract models that can be used to quickly and consistently add common fields and their functionalities to your models.

Installation

If using Astral's uv for virtual environment and project dependencies:

$ uv add gs-django-modelplus

Or, with pip in your virtual environment:

$ pip install gs-django-modelplus

Quick setup

Add application to settings.py:

INSTALLED_APPS = [
    ...
    "modelplus",
]

If using abstract model UserstampableModel, CurrentUserMiddleware has be added to settings.py after django.contrib.auth.middleware.AuthenticationMiddleware:

MIDDLEWARE = [
    ...
    "modelplus.middleware.CurrentUserMiddleware",
]

Abstract models

ActivatableModel and ActivatableQuerySet

Frequently objects need to have some kind of active flag, because deleting records is not an option.

Abstract model ActivatableModel defines is_active field for this purpose, which is True by default.

ActivatableQuerySet is a corresponding QuerySet that defines filters active and inactive for models which have ActivatableModel as parent.

class MyModel(ActivatableModel):
    ...

    objects = ActivatableQueryset.as_manager()

or:

class MyQueryset(ActivatableQueryset):
    ...


class MyModel(ActivatableModel):
    ...

    objects = MyQueryset.as_manager()

CancellableModel and CancellableQuerySet

Sometimes there is a need to mark some objects as cancelled.

Abstract model CancellableModel defines field is_cancelled, which is False by default.

CancellableQuerySet is a corresponding QuerySet that defines filters cancelled and not_cancelled for models which have CancellableModel as parent.

class MyModel(CancellableModel):
    ...

    objects = CancellableQueryset.as_manager()

or:

class MyQueryset(CancellableQueryset):
    ...


class MyModel(CancellableModel):
    ...

    objects = MyQueryset.as_manager()

CancellationModel and CancellationQuerySet

Sometimes there is a need to provide some additional information for cancelled objects.

Abstract model CancellationModel defines fields cancelled_at, cancelled_by and cancellation_reason, while CancellationQuerySet is a corresponding QuerySet that defines filters cancelled, not_cancelled and cancelled_by.

class MyModel(CancellationModel):
    ...

    objects = CancellationQueryset.as_manager()

or:

class MyQueryset(CancellationQueryset):
    ...


class MyModel(CancellationModel):
    ...

    objects = MyQueryset.as_manager()

LockableModel and LockableQuerySet

Sometimes there is a need to mark some objects as locked (not available for further editing).

Abstract model LockableModel defines field is_locked, which is False by default.

LockableQuerySet is a corresponding QuerySet that defines filters locked and unlocked for models which have LockableModel as parent.

class MyModel(LockableModel):
    ...

    objects = LockableQueryset.as_manager()

or:

class MyQueryset(LockableQueryset):
    ...


class MyModel(LockableModel):
    ...

    objects = MyQueryset.as_manager()

TimestampableModel

Sometimes there is a need to record when are objects created and last updated.

Abstract model TimestampableModel defines fields created_at and updated_at which store a timestamp when the model instance was created and last updated.

Timestamps are populated using pre_save signal timestampable. Unfortunately, creating and updating objects in bulk doesn't update created_by and modified_by fields, since pre_save is not called.

class MyModel(TimestampableModel):
    ...

UserstampableModel and UserstampableQuerySet

Sometimes there is a need to record who has created and last updated an object.

Abstract model UserstampableModel defines fields created_by and updated_by for storing a User instance which created and last updated the model instance. Deletion of the referenced created_by and modified_by objects is protected.

UserstampableQuerySet is a corresponding QuerySet that defines filters created_by and updated_by.

class MyModel(UserstampableModel):
    ...

    objects = UserstampableQueryset.as_manager()

or:

class MyQueryset(UserstampableQueryset):
    ...


class MyModel(UserstampableModel):
    ...

    objects = MyQueryset.as_manager()

This abstract model uses CurrentUserMiddleware which must be added to settings.MIDDLEWARE after django.contrib.auth.middleware.AuthenticationMiddleware.

Translations

Package comes with translations to:

  • Croatian (hr)

To create translations for new language:

$ git clone git@gitlab.com:gs-django-modelplus.git
$ cd gs-django-modelplus
$ uv sync
$ uv run django-admin makemessages -l language_code
# edit django.po file for the new language
$ make compilemessages
# build will also automatically compile messages
$ uv build

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

gs_django_modelplus-1.0.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gs_django_modelplus-1.0.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file gs_django_modelplus-1.0.0.tar.gz.

File metadata

  • Download URL: gs_django_modelplus-1.0.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.11

File hashes

Hashes for gs_django_modelplus-1.0.0.tar.gz
Algorithm Hash digest
SHA256 583b286522e92d7cc3b57a202ff2b31e90f09b3e3403bc389673e082d6e4fcb5
MD5 c17f10c3f9cbf4896ba4b67eb9990148
BLAKE2b-256 9926e8d86fbe91f1aec992e8301da14615beab5bafed23748dfd38cee0b75dba

See more details on using hashes here.

File details

Details for the file gs_django_modelplus-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gs_django_modelplus-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e28219556ba75b3ae04e60949e117fc3f401a5779b26ef024e96b7a7dd40f1cc
MD5 fc9571ce8a0c4c651240005dd4e7c930
BLAKE2b-256 ad530fcebb9b61356a925315174c1d25dce1c6fa4193a2c5c8b6db1aa0203372

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