Last active
February 4, 2018 13:35
-
-
Save shivan1b/7b4b47344a0d11880f84e87fa2908499 to your computer and use it in GitHub Desktop.
Django Model Choice constants using enum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Typo Error:- Second attribute of DIALECT class should be named as uk_english.