Skip to content

Instantly share code, notes, and snippets.

@shivan1b
Last active February 4, 2018 13:35
Show Gist options
  • Save shivan1b/7b4b47344a0d11880f84e87fa2908499 to your computer and use it in GitHub Desktop.
Save shivan1b/7b4b47344a0d11880f84e87fa2908499 to your computer and use it in GitHub Desktop.
Django Model Choice constants using enum
from django import models
from enum import Enum
class Language(models.Model):
class DIALECT(Enum):
us_english = ('us_en', 'US English')
uk_english = ('uk_en', 'UK English')
spanish = ('es', 'Spanish')
@classmethod
def get_val(cls, dialect):
return cls[dialect].value[0]
dialect = models.CharField(max_length=5, choices=[x.value for x in DIALECT])
@kodekracker
Copy link

Typo Error:- Second attribute of DIALECT class should be named as uk_english.

@shivan1b
Copy link
Author

shivan1b commented Feb 4, 2018

@kodekracker Fixed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment