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.1.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.1-py2.py3-none-any.whl (8.6 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: django_enum_choices-0.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 4990dc161e33b38bc51199a4315578cc9c4deaeacea251dd14db21b87a416776
MD5 c8188e4244f5bff9d575026dd231137b
BLAKE2b-256 07b6fe879282e4ad8cd75af9c49cf50975e5f2bd0ab41dfe4c55376ec5d18ac9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_enum_choices-0.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.6 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.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f7db5a6af2a806358d9cb469ab68da11f3f4af2ad8b66dc7cd7363c0beacf34c
MD5 fdc13ffefa1a234b656f11d9d981c1cc
BLAKE2b-256 8f7479a469c77bfe0a5a6fa22aacaaacc7d28460589ceebcb6312cc66d22d0ee

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