Django Model Choices
Django Model Choices (DMC) provides a readable and DRY way to specify choices for Django models and forms. Although there are many similar projects available, Django Model Choices is willing to offer the most neat and DRY syntax.
Features
A classic choices class in Django looks like this:
class CharityType(models.Model):
SOCIAL = 'SOC'
ENVIRONMENTAL = 'ENV'
EDUCATIONAL = 'EDU'
CHOICES = (
(SOCIAL, 'Social'),
(ENVIRONMENTAL, 'Environmental'),
(EDUCATIONAL, 'Educational'),
)
This is far from being DRY and is also prone to typos and other mistakes. Using Django Model Choices you could do:
from modelchoices import Choices
class CharityType(Choices):
SOCIAL = ('SOC', 'Social')
ENVIRONMENTAL = ('ENV', 'Environmental')
EDUCATIONAL = ('EDU', 'Educational')
The value of each choice must be a tuple containing a value for the database and a user readable value, which could also be translation object as in this example.
The CHOICES field will be generated magically by the metaclass, so you can use it in the model like this:
class Charity(models.Model):
charity_type = models.CharField(max_length=1, choices=CharityType.CHOICES)
The variables SOCIAL, ENVIRONMENTAL and EDUCATIONAL can be used to programmatically represent the choice values. Once the class is created, their values are no longer a tuples, but only the database fields (so CharityType.ENVIRONMENTAL is equal to 'ENV'). So you can do:
charity = Charity()
charity.charity_type = CharityType.ENVIRONMENTAL
charity.save()
Another field generated magically is VALUE_MAPPER. It is a dictionary, where the keys represent the database values and the dictionary values represent the variable name used to declare the options. In our example VALUE_MAPPER look like this:
>>> CharityType.VALUE_MAPPER
{
'SOC': 'SOCIAL',
'ENV': 'ENVIRONMENTAL',
'EDU': 'EDUCATIONAL'
}
This is useful if you need to use the variable names a codes (for a REST API for instance), because the user readable values might be subjects of translation.
Metadata
Release files for django-model-choices 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-model-choices-1.0.0.tar.gz | 2.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_model_choices-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.4 kB
Release files / django-model-choices-1.0.0.tar.gz
| Download URL | django-model-choices-1.0.0.tar.gz |
|---|---|
| Size | 2.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
da9c2904870db241c3a8394c7bd5fedc0cc6313af496b4adfd9c39feb7119113
|
|
BLAKE2b-256 checksum How to use checksums |
0cb9adf5d72c21eabd7bc4e89d783f2758ec3e7695635cc5b81ac524c36b6fbf
|
| 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/40.8.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
|
Release files / django_model_choices-1.0.0-py3-none-any.whl
| Download URL | django_model_choices-1.0.0-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9e1bce995202868debcac17b99782554a371bab78cc8c053aa90a8ab36ef1aaa
|
|
BLAKE2b-256 checksum How to use checksums |
157d1d76b97d4ccb0c850275a9748c62813fa3cd19b4d11e50618b060bb54d27
|
| 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/40.8.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
|