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

Library is still work in progress & unstable. When officially launched, we'll announce it.

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.4.tar.gz (7.2 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.4-py2.py3-none-any.whl (9.8 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: django_enum_choices-0.0.4.tar.gz
  • Upload date:
  • Size: 7.2 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.4.tar.gz
Algorithm Hash digest
SHA256 ee0d1ad3b1930d3d89178b37fba6a1a69817d381734607834dc907adf8f3a87c
MD5 6220024311ff51d26ad2464acd58a0ae
BLAKE2b-256 6670ac7fd785251e09edee72ef88ed3ab143bb3e8577dc1f44ceb0e7cd778512

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_enum_choices-0.0.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.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.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f82bcc5165cf2c8478f0b4bd22d7aca8588f5cacb47b436b149b798c4fe0a0a4
MD5 9b9a27561a5968d20efe924b0c38934c
BLAKE2b-256 0a8dda697c096c5cad153de6acbfe3f4bb40a27b237593f36909ca4a58dc07fb

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