class TaskStatus(models.Model):
CANCELLED = 0 REQUIRES_ATTENTION = 1 WORK_IN_PROGRESS = 2 COMPLETE = 3 STATUS_TYPES = ( (CANCELLED, 'Cancelled'), (REQUIRES_ATTENTION, 'Requires attention'), (WORK_IN_PROGRESS, 'Work in progress'), (COMPLETE, 'Complete'), ) status = models.IntegerField(choices=STATUS_TYPES, default=REQUIRES_ATTENTION) class Task(models.Model): status = models.ManyToManyField(TaskStatus, default=TaskStatus.REQUIRES_ATTENTION)
For global constants, I would second the strategy that Mike outlined, and rename your constants in a more specific way, such as TASK_CANCELLED, TASK_COMPLETE, etc. unless of course CANCELLED can apply to more than just tasks.
The use of a separate class to store such constants is of course viable, but seems like a bit of unnecessary complication if all it does is hold values. I wouldn't recommend it unless the class had other purposes such as returning computed values based on those stored constants.
I usually store model choices directly in the model, since they don't generally apply anywhere else.
Whatever you do, try to keep it consistent.
-James
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/54EADFC3.3020301%40dewhirst.com.au.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciVgDVT2vreL41GjKBUV5MmxDWhwMWSM8Pg92-YCeNMrng%40mail.gmail.com.
>>> In my case, a Task can have one or more TaskStatus. I do not wish to store
>>> the TaskStatus in the database since they are simply constants.
>>>
>>> Does anyone have a better way of how I can approach this?
>>>
I just read this a bit closer. I think what you mean is that you don't want a separate enumeration DB table that lists all of your choices with their integer codes and then your statuses link to it via FK's, etc. That's perfectly fine, and that should be how it works since you have enumerated all of the possible outcomes in your tuples. Since you have provided a list of choices to Django, it will validate that only those choices are allowed when forms are submitted, etc. as a bonus.
TL;DR; Unless you've hit a specific problem, it looks like you are doing it right.
https://docs.djangoproject.com/en/1.7/ref/models/fields/#choices
-James