[Answered ]-What is the best approach to keep many different django model type choices DRY?

2👍

It depends on your intended use of the code.

The goal in decoupling apps is to isolate functionality that is expected to be generally reusable across many Django projects. However, you mentioned that:

"…your apps are not going to be reused in other projects anyway"

If that is the case, especially if the apps aren’t considered to be good candidates for reuse, then strictly decoupling the apps isn’t necessary. Therefore, use a single Python file in the core of your app which contains the various choices lists used by multiple models.

Like this:

# choices.py
RESOURCE_CONTENT_CHOICES = (
    ...
)
EVENT_TYPE_CHOICES = (
    ...
)

And import where needed:

# models.py
from myproject.core.choices import EVENT_TYPE_CHOICES

class Event(models.Model):
    # Optional: See additional note below
    EVENT_TYPE_CHOICES = EVENT_TYPE_CHOICES
    ...
    event_type = models.IntegerField(choices=EVENT_TYPE_CHOICES)
    ...

Alternatively, if you discover some of your apps are indeed good candidates for reuse, then decoupling them is more important than DRY. In that scenario, you should maintain your choices inside that app, even if some choices are duplicated elsewhere in other apps.

Additional note: using either approach, it’s often very useful to have a reference to the choices within the model class. See: https://docs.djangoproject.com/en/stable/ref/models/fields/#choices

Leave a comment