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
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.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee0d1ad3b1930d3d89178b37fba6a1a69817d381734607834dc907adf8f3a87c |
|
MD5 | 6220024311ff51d26ad2464acd58a0ae |
|
BLAKE2b-256 | 6670ac7fd785251e09edee72ef88ed3ab143bb3e8577dc1f44ceb0e7cd778512 |
Close
Hashes for django_enum_choices-0.0.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f82bcc5165cf2c8478f0b4bd22d7aca8588f5cacb47b436b149b798c4fe0a0a4 |
|
MD5 | 9b9a27561a5968d20efe924b0c38934c |
|
BLAKE2b-256 | 0a8dda697c096c5cad153de6acbfe3f4bb40a27b237593f36909ca4a58dc07fb |