I am currently building a simple application in Django and am trying to settle on a good model design.
In the Django documentation and many blog posts I have read, the choice
keyword argument is recommended. This would be implemented in my (simplified) noun model like so:
class Noun(models.Model):
FEMININE = 0
MASCULINE = 1
NEUTER = 2
COMMON = 3
GENDER_TYPE = (
(FEMININE, "Feminine"),
(MASCULINE, "Masculine"),
(NEUTER, "Neuter"),
(COMMON, "Common"),
)
stem = models.CharField(max_length=200)
gender = models.IntegerField(choices=GENDER_TYPE, default=Noun.COMMON)
When I walked across the hallway and spoke to a db admin friend about how he would indicate enumerations in a database, he acknowledged debate but said that he would make a table for each enumeration and reference it by foreign key. This approach maps almost exactly to the following models:
class Gender(models.Model):
short_name = models.CharField(max_length=1) # M, F, N, or C
description = models.CharField(max_length=20) # Masculine, Feminine, Neuter, or Common
class Noun(models.Model):
stem = models.CharField(max_length=200)
gender = models.ForeignKey(Gender, on_delete=models.CASCADE)
I am trying to weigh the relative merits of each approach. Here are the pro's and con's I can discern:
First Approach
Second Approach
So, my question: Am I missing something obvious about why one approach is invalid? If both approaches are valid, which approach is better?
Both approaches are valid. To decide on one, you should consider mostly two points: how many choices you got, and how likely they will change in the future.
So, if there're few choices and they are unlikely to change often, use choices instead of foreign key - it will be better from performance point of view. If there're many choices or they change often - go for foreign key.
In case with genders, I would choose choices. Even if they change in future - existing ones most likely won't be affected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With