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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django_enum_choices-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4883cff62a7ef9f9be25a1bd143ef28726582137ab74519adac7749ccf04b812 |
|
MD5 | dbfa761f0927c70e82411eaae1ebe449 |
|
BLAKE2b-256 | 8b6b7c423a6fae3edd5beb95777ae62c21c5b2e986e44395ba93c577081adc6b |
Close
Hashes for django_enum_choices-0.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ec4d4d2cbfd9191781196ac9dab85d7b63f47fb73a001ae74000b75e2d6905d |
|
MD5 | f9ac9414fd857e969d1377bc13646ca5 |
|
BLAKE2b-256 | edf92deca2282616e45dd7c595c1c31b68f293b5a9f1180b55377ffa4f2e7595 |