A field that returns a python object.
Project description
django-register-field
A field that returns a python object.
Installation
To install, call pip install django-register-field
.
Usage
The RegisterField allows storing specific objects in the database and retriving them directly from the model. The will be stored as strings, with a Register that is used to keep track of which string maps to what object. It cannot be used to store objects dynamically, but can be very useful to separate some logic into their related classes, reducing the number of if/else required in the model methods.
There are 2 ways to implement the fields: through the RegisterChoices, or by manually implementing the register. The former makes it easy to implement and use, the latter offers more flexibility.
RegisterChoices
The implementation of the RegisterChoices is very similar to django's default Choices, but even simpler. It looks like this:
from dataclasses import dataclass
from django_register import RegisterChoices
@dataclass(unsafe_hash=True)
class MyOptions:
some_field: str
some_description: str
class SomeRegisterChoices(RegisterChoices):
OPTION_1 = MyOptions(some_field='field_name', some_description='field_description')
OPTION_2 = MyOptions(some_field='field_name_2', some_description='field_description_2')
SomeRegisterChoices
is now usable in a RegisterField on a model:
from django_register import RegisterField
class SomeModel(models.Model):
my_field = RegisterField(choices=SomeRegisterChoices.choices)
Note that the objects passed to RegisterChoices must be hashable. This is because the register keeps track of the relationship between the label and the object in both directions, so the object needs to be usable as a key in a dictionary.
By default, the label used in the database will be the same as the variable name on the choices, in lower case. This can be changed by having a label
attribute on the object. If one is set, that is what will be used database side. Similarly, the verbose field used in the .choices
to be displayed in django admin will be the variable name with all underscores replaced by a space, and .title
applied to it. This can be changed by setting the verbose_name
attribute on the object.
In the background, the RegisterChoices takes care of setting and handling the Register for you. You can also create it and set it manually, if using the Choices is not an option.
Setting the Register directly
To set the register manually, you first need to instantiate a Register:
from django_register import Register
register = Register()
With that done, you can register objects within your models.py
file directly.:
register.register(some_object, db_key='some_label')
db_key
is not required. If not set, the label
must be set on the object, otherwise a ValueError
is raised. You can then pass the register to the RegisterField
directly:
from django_register import RegisterField
class SomeModel(models.Model):
my_field = RegisterField(register=register)
You can also have it be part of the Model class:
from django_register import RegisterField
class SomeModel(models.Model):
register = Register()
register.register(some_object, db_key='some_label')
my_field = RegisterField(register=register)
Note that if using that technique, you are responsible for keeping track of your object. The RegisterChoices
make it easier to keep your objects for comparison and use them outside of the model, but both methods will give the same results database side.
If you need to set the register values dynamically, you can do so after the fact by using the register directly. However in that case, you need to provide a max_length
if your database does not support having a CharField
without a max_length
. That is because in the background, a CharField
is used to store the key in the database.
You can set the values dynamically like this:
class SomeApp(AppConfig):
def ready(self):
register.register(some_object, db_key='some_label')
It does not have to be in the ready
method, values can be added to the register anywhere, however you should be very careful about where you allow adding values and when. If the value is not available somewhere in the code, it will throw a ValidationError
saying that the value cannot be found in the register.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-register-field-1.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e201e615ae3390163461a3b3847b91446e31e9d236dbaab5d0a6afa228fb93f9 |
|
MD5 | e5d829b88081b0813546b40cee00f105 |
|
BLAKE2b-256 | b4ef312f812d988638a0ae82bdcfd757428386df8d436518e1af4c7059ec63ca |
Hashes for django_register_field-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f26b9f262049240520f8dff9b3b34f79c2d17a7f979b49cc0a294a39720703e |
|
MD5 | 7e3f94f1a6041bbd6b7bef52b8e0f3e4 |
|
BLAKE2b-256 | 3fd6c4739ee4df451b2f312c773eac7774d573e1c4893ddc35d152b80ea354e7 |