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.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4990dc161e33b38bc51199a4315578cc9c4deaeacea251dd14db21b87a416776 |
|
MD5 | c8188e4244f5bff9d575026dd231137b |
|
BLAKE2b-256 | 07b6fe879282e4ad8cd75af9c49cf50975e5f2bd0ab41dfe4c55376ec5d18ac9 |
Close
Hashes for django_enum_choices-0.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7db5a6af2a806358d9cb469ab68da11f3f4af2ad8b66dc7cd7363c0beacf34c |
|
MD5 | fdc13ffefa1a234b656f11d9d981c1cc |
|
BLAKE2b-256 | 8f7479a469c77bfe0a5a6fa22aacaaacc7d28460589ceebcb6312cc66d22d0ee |