Skip to main content

Custom Django field for using enumerations of named constants

Project description

Thi package is based on django-enumfield. It was extended to make django form filters happy at the time of validating input for named attributes rather than for enumerated values like 1,2,…

https://img.shields.io/pypi/v/django-enumfield-named-choices.svg https://img.shields.io/pypi/l/django-enumfield-named-choices.svg https://img.shields.io/pypi/pyversions/django-enumfield-named-choices.svg https://img.shields.io/pypi/wheel/django-enumfield-named-choices.svg

Installation

Install django-enumfield-named-choices in your Python environment:

$ pip install django-enumfield-named-choices

Usage

Create an Enum-class and pass it as first argument to the Django model EnumField.

from django.db import models
from django_enumfield_named_choices import enum

class BeerStyle(enum.Enum):
    LAGER = 0
    STOUT = 1
    WEISSBIER = 2

class Beer(models.Model):
    style = enum.EnumField(BeerStyle, default=BeerStyle.LAGER)
Beer.objects.create(style=BeerStyle.STOUT)
Beer.objects.filter(style=BeerStyle.STOUT)

You can use your own labels for Enum items

class Animals(enum.Enum):
    CAT = 1
    DOG = 2

    labels = {
        CAT: 'Cat',
        DOG: 'Dog'
    }

The Enum-class provides the possibility to use transition validation.

from django.db import models
from django_enumfield_named_choices import enum

class PersonStatus(enum.Enum):
    ALIVE = 1
    DEAD = 2
    REANIMATED = 3

    _transitions = {
        DEAD: (ALIVE,),
        REANIMATED: (DEAD,)
    }

class Person(models.Model):
    status = enum.EnumField(PersonStatus)

These transitions state that a PersonStatus can only go to DEAD from ALIVE and to REANIMATED from DEAD.

person = Person.objects.create(status=PersonStatus.ALIVE)
try:
    person.status = PersonStatus.REANIMATED
    person.save()
except InvalidStatusOperationError:
    print("Person status can not go from ALIVE to REANIMATED")

The Enum-class can also be used without the EnumField. This is very useful in Django form ChoiceFields.

from django.forms import Form
from django_enumfield_named_choices import enum

class GenderEnum(enum.Enum):
    MALE = 1
    FEMALE = 2

    labels = {
        MALE: 'Male',
        FEMALE: 'Female',
    }

class PersonForm(forms.Form)
    gender = forms.TypedChoiceField(choices=GenderEnum.choices(), coerce=int)

Rendering PersonForm in a template will generate a select-box with “Male” and “Female” as option labels for the gender field.

If you want to use this package along with django restful framework and django-filter, django-url-filter, and djangorestframework-filters packages to make filtering on the named values of Enum type instead of their numerical counterparts you can use extra attribute on your enum type interface with value type str, by default it is set to int type as following.

# in enums.py

from django_enumfield_named_choices import enum

class GenderEnum(enum.Enum):
    MALE = 1
    FEMALE = 2

    labels = {
        MALE: 'Male',
        FEMALE: 'Female',
    }

    interface = str

# in models.py

from django_enumfield_named_choices.db.fields import EnumField

class Person(models.Model):
name = ...
gender = EnumField(GenderEnum)

# and then when you expose you model through API endpoint
# you can filter it with following URL request
# /person/?gender=male
# instead of
# /person/?gender=1
# thought the actual values of enum in the database are still integers.

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-enumfield-named-choices-1.0a2.tar.gz (26.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_enumfield_named_choices-1.0a2-py2.py3-none-any.whl (37.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-enumfield-named-choices-1.0a2.tar.gz.

File metadata

  • Download URL: django-enumfield-named-choices-1.0a2.tar.gz
  • Upload date:
  • Size: 26.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.3

File hashes

Hashes for django-enumfield-named-choices-1.0a2.tar.gz
Algorithm Hash digest
SHA256 5f64846c74b61e5a893db8fbc8f37851d8abd90e3d9b2c30ed470e564843efe4
MD5 b349a817dafa244ca03a7e9166814529
BLAKE2b-256 92622b1c8552caefe62781ab1fa971029ebfe331a66046cbff0e075a74dca001

See more details on using hashes here.

File details

Details for the file django_enumfield_named_choices-1.0a2-py2.py3-none-any.whl.

File metadata

  • Download URL: django_enumfield_named_choices-1.0a2-py2.py3-none-any.whl
  • Upload date:
  • Size: 37.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.3

File hashes

Hashes for django_enumfield_named_choices-1.0a2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f9ee98e22d34111b48d123bc09ad3db34f7870e67dbead4afdb73265ccbfa259
MD5 ec9b55c14f7596ce0a9a77b553aa16ce
BLAKE2b-256 a3151e8a603515df4665fac730a576ccf9a3efd702a74f7dd197631eeec45fff

See more details on using hashes here.

Supported by

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