Skip to main content

Django Model Choices provides a neat and DRY way to specify the `choices` option for a Django models and forms.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-model-choices-1.0.0.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

django_model_choices-1.0.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file django-model-choices-1.0.0.tar.gz.

File metadata

  • Download URL: django-model-choices-1.0.0.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for django-model-choices-1.0.0.tar.gz
Algorithm Hash digest
SHA256 da9c2904870db241c3a8394c7bd5fedc0cc6313af496b4adfd9c39feb7119113
MD5 3af59be7417ee471feee77cca9baf72f
BLAKE2b-256 0cb9adf5d72c21eabd7bc4e89d783f2758ec3e7695635cc5b81ac524c36b6fbf

See more details on using hashes here.

File details

Details for the file django_model_choices-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: django_model_choices-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for django_model_choices-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e1bce995202868debcac17b99782554a371bab78cc8c053aa90a8ab36ef1aaa
MD5 f05efc6ea03fb7eeed8d7eb6f251c35a
BLAKE2b-256 157d1d76b97d4ccb0c850275a9748c62813fa3cd19b4d11e50618b060bb54d27

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page