Constants values and best practices

454 views
Skip to first unread message

Julo Waks

unread,
Aug 28, 2014, 1:04:28 PM8/28/14
to django...@googlegroups.com
Good day! 
How are you? 
I have the following situation: 

In more than one django project I come across the situation where I need to define a model that has a State.
So what I usually do is: 

# Pseudo code here 

def State (models.Model): 
     name = models.CharField () 

def Purchase (models.Model): 
     # Fields various (name, descrption, etc) 
     State = models.ForgeinKey (State) 
     # More fields ... 
    
The problem arises when I want to do things like: 

if (Purchase.State_id == 1): 

    # Do stuff 


my way to fix it so far was to define a const.py and do something like: 

if (== Purchase.State_id Const.STATE_EGGS): 

    # Do stuff 

Because the state 1 is "eggs", but because I add in my local, but if another dev adds in staging, as that uniformity is maintained? 
It should create a process that creates the updates as new constants was defined? 
There are best practices for this? 

Ideas? Advice? 

Thank you very much for reading =) 

regards, 
Julian.

yati sagade

unread,
Aug 28, 2014, 1:16:13 PM8/28/14
to django...@googlegroups.com
Hi Julian

Python 3 introduces enums, which should make this easy:
https://docs.python.org/3/library/enum.html
In my Python 2 projects, I define a class to club together related
constants. Something like,

class PurchaseState(object):
EGGS = 'eggs'
FOO = 'foo'
BAR = 'bar'

and then do

if purchase.state == PurchaseState.EGGS:
# do stuff

Note that I prefer using string values for my constants over integer
values, as it makes debugging far more pleasant.

Cheers
> --
> 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...@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/CAKSEZ1EfdSiaNwd6ZxL0NUhfn3HM39zdbg1CVkRXNyObwLxjcA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
Yati Sagade

Software Engineer at mquotient

Twitter: @yati_itay | Github: yati-sagade

Organizing member of TEDx EasternMetropolitanBypass
http://www.ted.com/tedx/events/4933
https://www.facebook.com/pages/TEDx-EasternMetropolitanBypass/337763226244869

Andrew Pinkham

unread,
Aug 28, 2014, 3:56:40 PM8/28/14
to django...@googlegroups.com
Hi Julian,

I think there are a couple options.

The easiest would be to store the constants as Python. I would recommend using the choices field, as detailed by the documentation:

https://docs.djangoproject.com/en/1.7/ref/models/fields/#choices

If you must store your constants as part of the database, and would like to keep the constants up to date across multiple databases/developers, you should look into data migrations. South provides them, as do the new migrations in Django 1.7 (I believe).

http://south.readthedocs.org/en/latest/tutorial/part3.html

Hope that helps,
Andrew

Reply all
Reply to author
Forward
0 new messages