• Stars
    star
    145
  • Rank 254,144 (Top 6 %)
  • Language
    Python
  • License
    Other
  • Created about 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Real Python Enums for Django

This package lets you use real Python (PEP435-style) enums with Django.

https://travis-ci.org/hzdg/django-enumfields.svg?branch=master

Installation

  1. pip install django-enumfields

Included Tools

EnumField, EnumIntegerField

from enumfields import EnumField
from enum import Enum

class Color(Enum):
    RED = 'r'
    GREEN = 'g'
    BLUE = 'b'

class MyModel(models.Model):

    color = EnumField(Color, max_length=1)

Elsewhere:

m = MyModel.objects.filter(color=Color.RED)

EnumIntegerField works identically, but the underlying storage mechanism is an IntegerField instead of a CharField.

Usage in Forms

Call the formfield method to use an EnumField directly in a Form.

class MyForm(forms.Form):

    color = EnumField(Color, max_length=1).formfield()

Enum

Normally, you just use normal PEP435-style enums, however, django-enumfields also includes its own version of Enum with a few extra bells and whistles. Namely, the smart definition of labels which are used, for example, in admin dropdowns. By default, it will create labels by title-casing your constant names. You can provide custom labels with a nested "Labels" class.

from enumfields import EnumField, Enum  # Our own Enum class

class Color(Enum):
    RED = 'r'
    GREEN = 'g'
    BLUE = 'b'

    class Labels:
        RED = 'A custom label'

class MyModel(models.Model):
    color = EnumField(Color, max_length=1)

assert Color.GREEN.label == 'Green'
assert Color.RED.label == 'A custom label'

EnumFieldListFilter

enumfields.admin.EnumFieldListFilter is provided to allow using enums in list_filter.

from enumfields.admin import EnumFieldListFilter

class MyModelAdmin(admin.ModelAdmin):
    list_filter = [('color', EnumFieldListFilter)]

Django Rest Framework integration

EnumSupportSerializerMixin mixin allows you to use enums in DRF serializers.

# models.py
from enumfields import EnumField
from enum import Enum

class Color(Enum):
    RED = 'r'
    GREEN = 'g'
    BLUE = 'b'

class MyModel(models.Model):
    color = EnumField(Color, max_length=1)


# serializers.py
from enumfields.drf.serializers import EnumSupportSerializerMixin
from rest_framework import serializers
from .models import MyModel

class MyModelSerializer(EnumSupportSerializerMixin, serializers.ModelSerializer):
    class Meta:
        model = MyModel
        fields = '__all__'

More Repositories

1

react-imageloader

A React component for wrangling image loading
JavaScript
353
star
2

gsap-react-plugin

A GSAP plugin for tweening React.js component state.
CoffeeScript
130
star
3

react-google-analytics

Google analytics component
CoffeeScript
111
star
4

django-staticbuilder

Add a build step to your static asset flow.
Python
76
star
5

django-ecstatic

An expansion pack for django.contrib.staticfiles!
Python
70
star
6

django-periodically

Python
39
star
7

reactdi

Dependency injection for ReactJS components.
CoffeeScript
27
star
8

react-pressable

Add onRelease, onReleaseInside, onReleaseOutside, and onPress events to a component
JavaScript
18
star
9

react-controlfacades

Style form controls but use their default behavior
JavaScript
13
star
10

urllite.js

A tiny, tiny URL parser.
JavaScript
11
star
11

react-loadqueueloader

A React component for managing loads with a load queue
CoffeeScript
11
star
12

react-loadermixin

A React mixin for giving components loading behavior
CoffeeScript
10
star
13

bedrock

a highly unopinionated and minimal front-end toolset built in LESS
CSS
7
star
14

django-galleries

The main django-galleries project.
Python
6
star
15

django-modeltools

Enums in your models, template strings for FileField names…
Python
5
star
16

django-mailmate

Define emails with classes and use templates for HTML emails.
Python
5
star
17

react-code39svg.js

Render Code 39 Barcodes as SVGs with React
JavaScript
4
star
18

linter-configs

A repository of config files for all the linters we use
JavaScript
4
star
19

react-qrsvg.js

Render QR codes as SVGs with React
JavaScript
3
star
20

timeout-after.js

Easily add timeouts to any function
JavaScript
3
star
21

cas-maestro

WordPress.org Plugin Mirror
PHP
3
star
22

django-google-search

Django app to show google site search results
Python
3
star
23

queueup.js

A promise-based JavaScript asset loader.
JavaScript
2
star
24

dirtyhex

Hexadecimal Naughtiness
2
star
25

django-combocache

Write-through caching for Django
Python
1
star
26

django-memcachedkeys

Sanitize your keys, please.
Python
1
star
27

disallow-new.js

Don't let people use "new" on your function
JavaScript
1
star
28

drf-url-content-type-override

Python
1
star
29

as-factory.js

Create factory functions from classes!
JavaScript
1
star
30

fabric-envclasses

Set up your fabric environments with a more declarative syntax
Python
1
star