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.3.tar.gz (6.3 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.3-py2.py3-none-any.whl (8.8 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: django_enum_choices-0.0.3.tar.gz
  • Upload date:
  • Size: 6.3 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.3.tar.gz
Algorithm Hash digest
SHA256 d064297e17239282574e6e354bf45387d9863fd2c274463318feb802f7619142
MD5 7303a8786c11937b6d91a06287fa965b
BLAKE2b-256 8216fd8af58761bff2f904ae658efe4dfe466c6f3566d1cf5a38601fe7729c65

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_enum_choices-0.0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.8 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.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d4cb4373a673144c50c8964abb7e9a38261aca0a5b1680fa07f99f286b3cf028
MD5 dd1f451161940a6bbb2bc4e4429ed099
BLAKE2b-256 7f6a19f232608f4b7913281bee74a70e7314553a6b98cc918eab378b0de0dd72

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