class PurchaseOrder(models.Model): po_number = models.IntegerField(unique=True)
def get_po_number(self, *args, **kwargs): if not self.id: last_po = PurchaseOrder.objects.order_by('po_number').last() if last_po: last_po_num = last_po.po_number[-2:] new_po_num = int(last_po_num) + 1 else: new_po_num = '0001' self.po_number = new_po_num return self.po_number
class PurchaseOrder(models.Model): po_number = models.IntegerField(unique=True, default=get_po_number(self))
Hi,
Aren't you looking for the AutoField?
https://docs.djangoproject.com/en/2.0/ref/models/fields/#autofield
It will automatically increment based on each record added to the table.
As an aside, the problem you are having is that in python, the
first argument to a class is a reference to the instance of that
class. In C this is often called a context. In C++ and Java it
would be like the `this` pointer. So you wouldn't generally pass
"self" to a class member.
Hope that helps!
--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6183c81e-54b8-4779-9bf3-c8c9e734f248%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
This message has been scanned for viruses and dangerous content by
E.F.A. Project, and is believed to be clean.
Click here to report this message as spam.
From
"https://docs.djangoproject.com/en/2.0/topics/db/models/#automatic-primary-key-fields",
it has a further definition of automatic primary key fields. In
your model just add "(primary_key=True)"
to po_number field.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/945d5f35-e98b-b1f1-a527-32ff83687668%40linear-systems.com.
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/wpq8f815GGE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a37859a4-3c35-6f19-6a9b-ef997361bcee%40gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGJrYjaMM7NdLHBXYxtzh5Ysii6qeepVTAGN2k3MHmZhnEmkrw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHV4E-dMGdsaZHNjAfYjqBn4LJnRKCO5TNqw34hP%2BAg%3D2KkRfg%40mail.gmail.com.