Skip to main content

A custom Django field able to use subclasses of Python's internal `Enum` class as choices

Project description

django-enum-choices

A custom Django field able to use subclasses of Python's internal Enum class as choices

Installation

git clone https://github.com/slavov-v/django-enum-choices.git
pip install -e some-directory/django_enum_choices

Basic Usage

# enums.py

from enum import Enum

class FooBarEnumeration(Enum):
    FOO = 'foo'
    BAR = 'bar'
# models.py

from django import models

from django_enum_choices.fields import EnumChoiceField

from .enums import FooBarEnumeration

class MyModel(models.Model):
    foo_bar_field = EnumChoiceField(enum_class=FooBarEnumeration)
# python manage.py shell

In [1]: instance = MyModel.objects.create(foo_bar_field=FooBarEnumeration.BAR)
In [2]: instance.foo_bar_field
Out[2]: <FooBarEnumeration.BAR: 'bar'>

Usage with Django Rest Framework

# serializers.py

from rest_framework import serializers

from django_enum_choices.serializers import EnumChoiceField

class MyModelSerializer(serializers.ModelSerializer):
    foo_bar_field = EnumChoiceField(enum_class=FooBarEnumeration)

    class Meta:
        model = MyModel
        fields = ('foo_bar_field', )
# python manage.py shell

In [1]: instance = MyModel.objects.create(foo_bar_field=FooBarEnumeration.BAR)
In [2]: MyModelSerializer(instance).data
Out[2]: {'foo_bar_field': 'bar'}

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_enum_choices-0.0.2.tar.gz (5.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_enum_choices-0.0.2-py2.py3-none-any.whl (8.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django_enum_choices-0.0.2.tar.gz.

File metadata

  • Download URL: django_enum_choices-0.0.2.tar.gz
  • Upload date:
  • Size: 5.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/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

File hashes

Hashes for django_enum_choices-0.0.2.tar.gz
Algorithm Hash digest
SHA256 4883cff62a7ef9f9be25a1bd143ef28726582137ab74519adac7749ccf04b812
MD5 dbfa761f0927c70e82411eaae1ebe449
BLAKE2b-256 8b6b7c423a6fae3edd5beb95777ae62c21c5b2e986e44395ba93c577081adc6b

See more details on using hashes here.

File details

Details for the file django_enum_choices-0.0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: django_enum_choices-0.0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.2 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/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

File hashes

Hashes for django_enum_choices-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7ec4d4d2cbfd9191781196ac9dab85d7b63f47fb73a001ae74000b75e2d6905d
MD5 f9ac9414fd857e969d1377bc13646ca5
BLAKE2b-256 edf92deca2282616e45dd7c595c1c31b68f293b5a9f1180b55377ffa4f2e7595

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