Enum using Django ModelField as a string - anti-pattern?

I have a Django field, which I use mainly as an enumeration for notification settings.

Now I configured it like this:

class MyModel(models.Model):
    # ...
    EVERY_TIME = 'every'; WEEKLY = 'weekly'; NEVER = 'never'
    NOTIFICATION_CHOICES = ((EVERY_TIME, "Every time"), (WEEKLY, "Weekly"), (NEVER, "Never"))
    notification_preferences = models.CharField(choices=NOTIFICATION_CHOICES, default=EVERY_TIME, max_length=10)

I know that usually this kind of enumeration should be configured as models.IntegerField, rather than CharField, but since the front-end uses Angular and all the data is transmitted through the API, it seems to me that it can provide a little more useful information for the front-end for receiving 'weekly', rather than 2, eg.

Is it bad practice to use CharFieldas an enumeration? If so, is my use case small enough for it to be unimportant, or is there something I am missing to change it?

+4
source share
4 answers

You might want to consider the Django-model-utilsChoices library , which will give you more control over the text versions of the enumeration.

To answer your question, not everything lends itself to an integer identifier. Consider the states, Australia has 7, and they have a fixed when the set is known:

ACT - Australian Capital Territory
NSW - New South Wales
NT  - Northern Territory
QLD - Queensland
SA  - South Australia
TAS - Tasmania
VIC - Victoria
WA  - Western Australia

Since they are relatively fixed (the composition of the country is unlikely to change, there is no reason to assign an integer to each when a text encoding with a full name works just as well.

I would not say that using CharFieldas a choice is an anti-pattern, just an approach that should be applied only if you are sure that shortened versions of the database make sense when stored as text.

Enum

from enum import Enum

class Countries(Enum):
   ACT = "Australian Capital Territory"
   NSW = "New South Wales"
   ...

class MyModel(models.Model):
    country = models.CharField(choices=[(tag.name, tag.value) for tag in Countries])
+4

CharField ? , , , - , ?

. , . , , CharField - , , , :

" ?"

, , , , , , .

, , , - , , . .

, models.IntegerField, CharField, front-end Angular, API, , , "", 2.

, , , . , , , , .

API, . - , , .

, ; Python. , python.

+2

CharField , , -, , Django choices, :

FRESHMAN = 'FR'
SOPHOMORE = 'SO'
JUNIOR = 'JR'
SENIOR = 'SR'
YEAR_IN_SCHOOL_CHOICES = (
    (FRESHMAN, 'Freshman'),
    (SOPHOMORE, 'Sophomore'),
    (JUNIOR, 'Junior'),
    (SENIOR, 'Senior'),
)
year_in_school = models.CharField(max_length=2,
                                  choices=YEAR_IN_SCHOOL_CHOICES,
                                  default=FRESHMAN) 
0

Source: https://habr.com/ru/post/1547860/


All Articles