Django will not allow you to have more than one AutoField in the model, and you already have one for your primary key. Thus, you will have to redefine the persistence and probably have to look back at the table to figure out what needs to be increased.
Something like that:
class Product(models.Model): code = models.IntegerField() number = models.IntegerField() ... def get_serial_number(self): "Get formatted value of serial number" return "%.2d-%.3d" % (self.code, self.product) def save(self): "Get last value of Code and Number from database, and increment before save" top = Product.objects.order_by('-code','-number')[0] self.code = top.code + 1 self.number = top.number + 1 super(Product, self).save()
Please note that without any lock in your save method, you may run into the concurrency problem (two threads try to keep the same values ββin the code and number).
Seth
source share