django-admin-fieldoverride
A system for allowing a local model to override readonly fields from other source, e.g., from an API (PBSMM, FMP)
Background
For project that use external APIs (e.g, PBSMM or FMP), the content that is pulled is meant to be read-only. However sometimes, producers want to override specific fields for specific instances: e.g., for a PBSMMEpisode record, they might want to use a different description than the one that comes from the API.
Typically, things are set up so that there's a "local" model instance that has a 1:1 relationship to the record obtained from the API, e.g., for PBSMM there's a PBSMMEpisode model that has a description_long field. One COULD just create all of the fields in the local model, and then set up methods to peruse them when generating content in a view. However that will quickly get messy because a) for most cases, an override won't be needed; and b) you end up coding all those extra fields "just in case" without absolutely knowing that they'll ever be used.
This makes the Admin full of cruft which gets in the way of content management.
Enter Field Overrides
What if - instead - you had a single "override" field that has a FK to the "local" model instance, so that you only have to use it WHEN you need to.
In this siutation, the Admin is simplified: you just have an Inline admin for ALL of the overrides for that record.
How it works
You can get the list of fields for any model class through ._meta.fields. Most of them probably will NOT want to be overridden
(e.g., the id, and other things like creation dates, etc.). So we'll have to come up with a way to filter those fields out.
For the remaining set, it would be a dropdown field in the Admin along with a field for the override content. Everything gets stored in
a table that also keeps track of which model is involved (so some kind of generic foreign key).
When processing the record, i.e., a view to generate a detail page, or a REST API endpoint, you'd go through the list of override-able fields, check to see if there is an override, and use it, or fall back to the original field from the API data.
(Note that for a project like Roadshow that would involve TWO sources, FMP and PBSMM, but we can use this setup to handle the order of precedence, AND we can also set it up that for any given overriden field that order can be different).
Installation
Creating the model map_override_fields method
Each model needs a map_override_fields method that returns a list of fields that can be overriden. Each item in the list is a dict that has the parameters of the override:
- field: the name of the field when overridden, e.g., 'description'
- overrides: this is a list of the fields that would be overridden, e.g., ["fmp__description", "pbsmm__description_long"]
They're used to populate the choices for the fields in the Override model which also has the field for the overriding content.
SO FAR, the only way I can think to handle the fact that these fields can have different types (e.g., Text versus Integer), is to store everything as a TextField, and then on processing re-cast the content as needed.
In the view
We'll probably want a mixin for this that sets things appropriately in the context.
Metadata
Release files for django-admin-fieldoverride 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django-admin-fieldoverride-0.3.1.tar.gz | 6.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_admin_fieldoverride-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.7 kB
Release files / django-admin-fieldoverride-0.3.1.tar.gz
| Download URL | django-admin-fieldoverride-0.3.1.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
95ad8b03356250793ab41e46598efd0516b1d0e65785cd41cb13cf822770e5a8
|
|
BLAKE2b-256 checksum How to use checksums |
10b0131fe04c92ab0777ef49fe959e5d2f01f7604ad61c678d4ff1301905bb6c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.5
|
Release files / django_admin_fieldoverride-0.3.1-py3-none-any.whl
| Download URL | django_admin_fieldoverride-0.3.1-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
88f070b816f32c621653caf456afd0552fba4b05eba13b5dba26dda714489549
|
|
BLAKE2b-256 checksum How to use checksums |
a6f325384112964bfd17cb4d01018a0db0c115fc6298c97fc37b7ebd67ecbe6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.5
|
Security for django-admin-fieldoverride 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.