model that hold named references to specific instances of any Django model.
Usage
=====
References are controlled by the :func:`register()` and :func:`load` functions of
the ``model_reference`` module. These function are also mirrored by the
corresponding methods in the ``Reference.objects`` manager. To register a
reference, simply pass the object and either a unique name or a unique suffix::
from model_reference import register, load
from django.contrib.auth.models import User
# Fetch some objects
john = User.objects.get(username='john')
ringo = User.objects.get(username='ringo')
# Register references in the database
register(john, 'favorite beatle')
register(ringo, suffix='drummer')
The name for each reference is unique. If the register function is called with
the explicit ``suffix`` argument, it will register the instance as
``<app_label>.<model>:suffix``, which in our case becomes ``'auth.user:drummer'``.
In order to retrieve the objects referred by the Reference object, we can use
the :func:`load` function::
>>> load('favorite beatle')
<User: john>
>>> load('drummer', model=User)
<User: ringo>
Objects registered with the suffix form of the register function should pass a
model class in order to construct the complete name string. This is equivalent
as creating the string by hand:
>>> load('auth.user:drummer')
<User: ringo>
Empty references
================
If one request a non-existing reference to the load() function, it raises an
:error:`model_reference.EmptyReferenceError`. Users can register factory
functions to create these objects on-the-fly.
The default way of registering factory function is via the :func:`model_reference.factory`
decorator. It must be called with a reference string (or suffix + model) and
decorates a function that return a new instance and is called without arguments::
@factory('drummer', model=User)
def make_ringo():
user, created = User.object.get_or_create(
username=ringo,
first_name='Richard'
last_name='Starkley',
email='ringo@applerecords.co.uk'
)
return user
If a new instance is created, the function is responsible to save it in the
database
Metadata
Release files for django-model-reference 0.1.2
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 | Actions |
|---|---|---|---|---|
| django-model-reference-0.1.2.tar.gz | 8.1 kB | Without provenance | Details for django-model-reference-0.1.2.tar.gz |
Release files / django-model-reference-0.1.2.tar.gz
| Download URL | django-model-reference-0.1.2.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b8d623fbf32083cd103e9f0e397967b4c47214a78455e903d4209d8544fd7705
|
|
BLAKE2b-256 checksum How to use checksums |
16908568d9ce16a0ee1f0412cf07c181848a104e3dcaf7ca225e3bd6ec07b45b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-model-reference 0.1.2
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.