Skip to content

Let model extensions define new fields

Nik | Klampfradler requested to merge feature/extensible-models into master

This MR extends the ExtensibleModel to accept fields in addition to properties and methods. It leverages django-jsonstore in order to aggregate those fields in one extended_data JSONField, so as to not need migrations when apps add additional fields. Apps should use this feature sparsely, and for more complex additions, add their own model with a ForeignKey instead.

The following example put in model_extensions.py of an app adds a field for the t-shirt size of a person:

from jsonstore import CharField

from .models import Person


Person.field(shirt_size=CharField(verbose_name="T-Shirt size", default="7XL"))

The fields are transparent to all known parts of Django. They are accessed like any other field, and even automagically show up in forms or a ModelAdmin:

Skjermdump_fra_2020-01-18_23-28-48

Filtering on JSONField is fully supported.

Advances #141 (closed).

Edited by Nik | Klampfradler

Merge request reports

Loading