All Projects → 5monkeys → Django Enumfield

5monkeys / Django Enumfield

Licence: mit
Custom Django field for using enumerations of named constants

Programming Languages

python
139335 projects - #7 most used programming language
enum
40 projects

Projects that are alternatives of or similar to Django Enumfield

Django Rest Framework Serializer Extensions
Extensions to help DRY up Django Rest Framework serializers
Stars: ✭ 180 (-2.17%)
Mutual labels:  django, django-rest-framework
Django Vue Admin
基于RBAC模型权限控制的中小型应用的基础开发平台,前后端分离,后端采用django+django-rest-framework,前端采用vue+ElementUI,移动端采用uniapp+uView(可发布h5和小程序).
Stars: ✭ 157 (-14.67%)
Mutual labels:  django, django-rest-framework
Django Trench
django-trench provides a set of REST API endpoints to supplement django-rest-framework with multi-factor authentication (MFA, 2FA). It supports both standard built-in authentication methods, as well as JWT (JSON Web Token).
Stars: ✭ 123 (-33.15%)
Mutual labels:  django, django-rest-framework
Sarenka
OSINT tool - gets data from services like shodan, censys etc. in one app
Stars: ✭ 120 (-34.78%)
Mutual labels:  django, django-rest-framework
Vue People
VuePeople lists and connects Vue.JS developers around the world.
Stars: ✭ 167 (-9.24%)
Mutual labels:  django, django-rest-framework
Lpoj
An open source online judge system base on Django REST framework and Vue.js !
Stars: ✭ 122 (-33.7%)
Mutual labels:  django, django-rest-framework
Django Structlog
Structured Logging for Django
Stars: ✭ 127 (-30.98%)
Mutual labels:  django, django-rest-framework
Bag Of Holding
An application to assist in the organization and prioritization of software security activities.
Stars: ✭ 114 (-38.04%)
Mutual labels:  django, django-rest-framework
Usaspending Api
Server application to serve U.S. federal spending data via a RESTful API
Stars: ✭ 166 (-9.78%)
Mutual labels:  django, django-rest-framework
Django Guid
Inject an ID into every log message from a Django request. ASGI compatible, integrates with Sentry, and works with Celery
Stars: ✭ 166 (-9.78%)
Mutual labels:  django, django-rest-framework
Paperweekly forum
paperweekly's forum
Stars: ✭ 118 (-35.87%)
Mutual labels:  django, django-rest-framework
Djangorestframework Queryfields
Allows clients to control which fields will be sent in the API response
Stars: ✭ 170 (-7.61%)
Mutual labels:  django, django-rest-framework
Hydroshare
HydroShare is a collaborative website for better access to data and models in the hydrologic sciences.
Stars: ✭ 117 (-36.41%)
Mutual labels:  django, django-rest-framework
Crud App Vuejs Django
This is simple crud app and searchFilter made using vuejs and django. Used to explain the tutorial present on https://medium.com/@shubhambansal_89125/crud-app-using-vue-js-and-django-516edf4e4217 https://medium.com/@shubhambansal_89125/searchfilter-using-django-and-vue-js-215af82e12cd
Stars: ✭ 174 (-5.43%)
Mutual labels:  django, django-rest-framework
Maria Quiteria
Backend para coleta e disponibilização dos dados 📜
Stars: ✭ 115 (-37.5%)
Mutual labels:  django, django-rest-framework
Django Microservices
UNMAINTAINED
Stars: ✭ 124 (-32.61%)
Mutual labels:  django, django-rest-framework
Crudl Example Django
CRUDL with Django, DRF/Graphene and SQLite
Stars: ✭ 113 (-38.59%)
Mutual labels:  django, django-rest-framework
Everbug
Django debug tool for browser
Stars: ✭ 114 (-38.04%)
Mutual labels:  django, django-rest-framework
Django Grpc Framework
gRPC for Django.
Stars: ✭ 162 (-11.96%)
Mutual labels:  django, django-rest-framework
Rest Api Basics
This is a basic guide on how to build a REST API with Django & Python. For much deeper depth, check out our new course on REST API: (https://kirr.co/90kxtx)
Stars: ✭ 171 (-7.07%)
Mutual labels:  django, django-rest-framework

django-enumfield

Provides an enumeration Django model field (using IntegerField) with reusable enums and transition validation.

Build Status PyPi Version License Python Versions Wheel Coveralls github

Installation

Currently, we test Django versions 1.11-3.1 and Python versions 2.7, 3.4-3.8.

Install django-enumfield in your Python environment:

$ pip install django-enumfield

Upgrading from django-enumfield 1.x? See the migration guide

For use with Django versions prior to 1.8 use version 1.2.1

For use with Django versions prior to 1.11 use version 1.5

Usage

Create an Enum-class and pass it as first argument to the Django model EnumField.

from django.db import models
from django_enumfield import enum


class BeerStyle(enum.Enum):
    LAGER = 0
    STOUT = 1
    WEISSBIER = 2


class Beer(models.Model):
    style = enum.EnumField(BeerStyle, default=BeerStyle.LAGER)


# Use .get to get enum values from either name or ints
print(BeerStyle.get("LAGER"))  # <BeerStyle.LAGER: 0>
print(BeerStyle.get(1))  # <BeerStyle.STOUT: 1>
print(BeerStyle.get(BeerStyle.WEISSBIER))  # <BeerStyle.WEISSBIER: 2>

# It's also possible to use the normal enum way to get the value
print(BeerStyle(1))  # <BeerStyle.STOUT: 1>
print(BeerStyle["LAGER"])  # <BeerStyle.LAGER: 0>

# The enum value has easy access to their value and name
print(BeerStyle.LAGER.value)  # 0
print(BeerStyle.LAGER.name)  # "LAGER"

For more information about Python 3 enums (which our Enum inherits, IntEnum to be specific) checkout the docs.

Setting the default value

You can also set default value on your enum class using __default__ attribute

from django.db import models
from django_enumfield import enum


class BeerStyle(enum.Enum):
    LAGER = 0
    STOUT = 1
    WEISSBIER = 2

    __default__ = LAGER


class BeerStyleNoDefault(enum.Enum):
    LAGER = 0


class Beer(models.Model):
    style_default_lager = enum.EnumField(BeerStyle)
    style_default_stout = enum.EnumField(BeerStyle, default=BeerStyle.STOUT)
    style_default_null = enum.EnumField(BeerStyleNoDefault, null=True, blank=True)


# When you set __default__ attribute, you can access default value via
# `.default()` method of your enum class
assert BeerStyle.default() == BeerStyle.LAGER

beer = Beer.objects.create()
assert beer.style_default_larger == BeerStyle.LAGER
assert beer.style_default_stout == BeerStyle.STOUT
assert beer.style_default_null is None

Labels

You can use your own labels for Enum items

from django.utils.translation import ugettext_lazy
from django_enumfield import enum


class Animals(enum.Enum):
    CAT = 1
    DOG = 2
    SHARK = 3

    __labels__ = {
        CAT: ugettext_lazy("Cat"),
        DOG: ugettext_lazy("Dog"),
    }


print(Animals.CAT.label)  # "Cat"
print(Animals.SHARK.label)  # "SHARK"

# There's also classmethods for getting the label
print(Animals.get_label(2))  # "Dog"
print(Animals.get_label("DOG"))  # "Dog"

Validate transitions

The Enum-class provides the possibility to use transition validation.

from django.db import models
from django_enumfield import enum
from django_enumfield.exceptions import InvalidStatusOperationError


class PersonStatus(enum.Enum):
    ALIVE = 1
    DEAD = 2
    REANIMATED = 3

    __transitions__ = {
        DEAD: (ALIVE,),  # Can go from ALIVE to DEAD
        REANIMATED: (DEAD,)  # Can go from DEAD to REANIMATED
    }


class Person(models.Model):
    status = enum.EnumField(PersonStatus)

# These transitions state that a PersonStatus can only go to DEAD from ALIVE and to REANIMATED from DEAD.
person = Person.objects.create(status=PersonStatus.ALIVE)
try:
    person.status = PersonStatus.REANIMATED
except InvalidStatusOperationError:
    print("Person status can not go from ALIVE to REANIMATED")
else:
    # All good
    person.save()

In forms

The Enum-class can also be used without the EnumField. This is very useful in Django form ChoiceFields.

from django import forms
from django_enumfield import enum
from django_enumfield.forms.fields import EnumChoiceField


class GenderEnum(enum.Enum):
    MALE = 1
    FEMALE = 2

    __labels__ = {
        MALE: "Male",
        FEMALE: "Female",
    }


class PersonForm(forms.Form):
    gender = EnumChoiceField(GenderEnum)

Rendering PersonForm in a template will generate a select-box with "Male" and "Female" as option labels for the gender field.

Local Development Environment

Make sure black and isort is installed in your env with pip install -e .[dev].

Before committing run make format to apply black and isort to all files.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].