On 23/02/2015 5:23 PM, Frankline wrote:
Hi all,

I am getting confused regarding the use of constants and would be keen to
know how the rest of you handle constants in your models. Ofcourse I could
handle it easily similar to how it has been handled here:
https://docs.djangoproject.com/en/1.7/ref/models/fields/#django.db.models.Field.choices


I have many many constants which are used across a number of modules and apps.

I store them in the __init__.py file of the namespace in which they are used. Sensible naming of constants is vital of course and I'm careful to store them alphabetically so I can find them easily enough scrolling quickly through the file.

At the top of my modules I say ...

from <namespace> import CONST1, CONST2 etc

Works for me

Mike

But I wanted to put the constants in a separate class on their own. This is
what I hav so far:

models.py

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)


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?


--
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to