Sunday, September 5, 2010

Generate a daily serial/batch/lot number

I have a project running in a manufacturing industry that is actually
built upon django.
In this I need to generate a unique serial, batch or lot number
(depending on what you would like to call it) that is a running number
from 0 to whathever for each and every day.
There is a high risk of concurrency so just finding the previous max
and then do a +1 before saving is not what I want.
The important part of the model looks like this...

class ProducedEntity(models.Model):
....
production_date = models.DateField(auto_now_add=True)
lot_no = models.PositiveIntegerField(default=0)

class Meta:
unique_together = ('production_date', 'lot_no')

Of course I could just save it and see if the .save() call works
without generating a IntegrityError but that's not elegant.
Is there a way of generating that number per day in a way that it's
done when I save the model?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment