https://github.com/2degrees/django-enum-field
A Django field for use with https://github.com/2degrees/python-enumeration.
https://github.com/2degrees/django-enum-field
Last synced: 2 months ago
JSON representation
A Django field for use with https://github.com/2degrees/python-enumeration.
- Host: GitHub
- URL: https://github.com/2degrees/django-enum-field
- Owner: 2degrees
- Created: 2016-10-24T15:51:42.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-04-11T15:16:54.000Z (about 9 years ago)
- Last Synced: 2025-12-31T00:31:47.986Z (6 months ago)
- Language: Python
- Size: 11.7 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://travis-ci.org/2degrees/django-enum-field)
[](https://coveralls.io/github/2degrees/django-enum-field?branch=master)
# django-enum-field
A Django field for use with [python-enumeration](https://github.com/2degrees/python-enumeration).
## Getting started
django-enum-field can be installed from PyPi via:
```bash
pip install django-enum-field
```
## Using in your models
```python
>>> from django.db.models import Model
>>> from enum_field import Enum, EnumField
>>> CLOTHING_SIZES = Enum(
... ('xs', 'EXTRA_SMALL'),
... ('s', 'SMALL'),
... ('m', 'MEDIUM'),
... ('l', 'LARGE'),
... ('xl', 'EXTRA_LARGE'),
... ('xxl', 'EXTRA_EXTRA_LARGE'),
... )
>>> class ClothingItem(Model):
... size = EnumField(CLOTHING_SIZES)
```
You can now use the enum in your look-ups:
```python
>>> ClothingItem.objects.filter(size=CLOTHING_SIZES.SMALL)
[...]
>>> ClothingItem.objects.filter(size__in=CLOTHING_SIZES.SMALL.subsequent_values)
```
You can set the field value to an enum item:
```python
>>> item = ClothingItem.objects.create(size=CLOTHING_SIZES.LARGE)
```
If you're using Django forms, you can specify UI labels for your enum
and they'll be used in any model forms:
```python
>>> # Before defning the model:
>>> CLOTHING_SIZES.set_ui_labels({
... CLOTHING_SIZES.EXTRA_SMALL: 'Extra small',
... CLOTHING_SIZES.SMALL: 'Small',
... CLOTHING_SIZES.MEDIUM: 'Medium',
... CLOTHING_SIZES.LARGE: 'Large',
... CLOTHING_SIZES.EXTRA_LARGE: 'Extra large',
... CLOTHING_SIZES.EXTRA_EXTRA_LARGE: 'Extra extra large',
... })
>>> ClothingItem._meta.get_field('size').choices
((, 'Extra small'), (, 'Small'), (, 'Medium'), (, 'Large'), (, 'Extra large'), (, 'Extra extra large'))
>>> # If you're not using ModelForm, but want the choices:
>>> CLOTHING_SIZES.get_ui_labels()
((, 'Extra small'), (, 'Small'), (, 'Medium'), (, 'Large'), (, 'Extra large'), (, 'Extra extra large'))
```
## Migrations
`EnumField` is full deconstructible and is, therefore, compatible with
Django's migration system. **Please note**, to make the underlying `Enum`
and `EnumItem` compatible with Django's migration system, you _must_
use the sub-classes available from `enum_field.Enum` and
`enum_field.EnumItem` and *not* from the `python-enumeration` project.
# Changelog
## Version 1.0 Beta 1 (2017-04-11)
- Fell back to using the enum values in the field `choices` if the UI labels
are not set.
- **Backwardly incompatible:** Dropped ability to set custom field choices.