Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Best way to implement "status" field in modules

Tags:

python

django

I have a field in my module that is used to hold the status of the object. So far I have used:

ORDER_STATUS = ((0, 'Started'), (1, 'Done'), (2, 'Error'))
status = models.SmallIntegerField(choices=ORDER_STATUS)

Its easy to convert one way:

def status_str(self): return ORDER_STATUS[self.status][1]

The problem is when updating. I find myself having code like this:

order.status = 2 # Error Status

Which is quite awful and gets really hard to synchronize. I guess a solution would be something similar to C's enum{}. Or perhaps there is a whole different way to tackle this problem ?

Thanks

like image 854
Boris Avatar asked Jan 26 '10 19:01

Boris


People also ask

What is __ str __ In Django model?

str function in a django model returns a string that is exactly rendered as the display name of instances for that model.

What is Onetoone field in Django?

This field can be useful as a primary key of an object if that object extends another object in some way. For example – a model Car has one-to-one relationship with a model Vehicle, i.e. a car is a vehicle. One-to-one relations are defined using OneToOneField field of django.

What does Django DB models model clean () do?

clean() This method should be used to provide custom model validation and to modify attributes on your model if desired.

How do I update a field in Django?

Use update_fields in save() If you would like to explicitly mention only those columns that you want to be updated, you can do so using the update_fields parameter while calling the save() method. You can also choose to update multiple columns by passing more field names in the update_fields list.


4 Answers

Maybe this question helps you: Set Django IntegerField by choices=… name.
I quote from the accepted answer (with adjustments ;)):
Put this into your class (STATUS_CHOICES will be the list that is handed to the choices option of the field):

PENDING = 0
DONE = 1
STATUS_CHOICES = (
    (PENDING, 'Pending'),
    (DONE, 'Done'),
)

Then you can do order.status = Order.DONE.


Note that you don't have to implement an own method to retrieve the (readable) value, Django provides the method get_status_display itself.

like image 158
Felix Kling Avatar answered Sep 19 '22 02:09

Felix Kling


what I usually do for this situation is:

models.py

from static import ORDER_STATUS    
status = models.PositiveSmallIntegerField(choices=ORDER_STATUS)

static.py

ORDER_STATUS = ((0, 'Started'), (1, 'Done'), (2, 'Error'))
ORDER_STATUS_DICT = dict((v, k) for k, v in ORDER_STATUS)

Now you can do:

from static import ORDER_STATUS_DICT
order.status = ORDER_STATUS_DICT['Error']
like image 20
fijter Avatar answered Sep 21 '22 02:09

fijter


This is a very late answer, however for completeness I should mention that django-model-utils already contains a StatusField and even better a StatusModel. I am using it everywhere I need to have a status.

like image 40
Serafeim Avatar answered Sep 21 '22 02:09

Serafeim


you can try enum package: http://pypi.python.org/pypi/enum/

like image 22
zaca Avatar answered Sep 21 '22 02:09

zaca