Hi, was wondering if we define all model choices in a single centralized file outside of models.py
, would there be any potential downside? Anyone can share their experience on this approach? Is this recommended?
Typical way of defining model choices
# models.py
from django.db import models
class ExampleClass(models.Model):
EXAMPLE_CHOICES = [
("CHOICE1", "CHOICE1"),
("CHOICE2", "CHOICE2"),
("CHOICE3", "CHOICE3"),
]
example_field = models.CharField(choices=EXAMPLE_CHOICES)
This works if choices is outside model class
# models.py
from django.db import models
EXAMPLE_CHOICES = [
("CHOICE1", "CHOICE1"),
("CHOICE2", "CHOICE2"),
("CHOICE3", "CHOICE3"),
]
class ExampleClass(models.Model):
example_field = models.CharField(choices=EXAMPLE_CHOICES)
Thinking of this
# choices.py
EXAMPLE_CHOICES = [
("CHOICE1", "CHOICE1"),
("CHOICE2", "CHOICE2"),
("CHOICE3", "CHOICE3"),
]
# models.py
from django.db import models
from centralizedbasefolder.choices import *
class ExampleClass(models.Model):
example_field = models.CharField(choices=EXAMPLE_CHOICES)
Reason for doing this is because it will be easier to access choices if you need to:
# instead of
> from example1app import ExampleClass
> from example2app import ExampleClass2
> from example3app import ExampleClass3
> ExampleClass.EXAMPLE_CHOICES
> ExampleClass2.EXAMPLE_CHOICES2
> ExampleClass3.EXAMPLE_CHOICES3
# you can do
> from centralizedbasefolder.choices import *
> EXAMPLE_CHOICES
> EXAMPLE_CHOICES2
> EXAMPLE_CHOICES3