you can use count() in views or models to count number of objects then add the new record
On Tue, 24 Jul 2018, 02:55 Alexander Joseph, <[email protected]> wrote: > Thanks Michael and Gerald. With an auto key field or primary key field > would users still be able to input their own value? And if so how would I > be able to give it a default attribute in a form that shows what the next > number should be, if the user decides to choose to use the auto increment? > Thanks again > > On Mon, Jul 23, 2018, 6:36 PM Gerald Brown <[email protected]> wrote: > >> From >> "https://docs.djangoproject.com/en/2.0/topics/db/models/#automatic-primary-key-fields" >> <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. >> >> On Tuesday, 24 July, 2018 07:54 AM, Michael MacIntosh wrote: >> >> 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! >> >> On 7/23/2018 1:39 PM, Alexander Joseph wrote: >> >> Hello, >> >> I have a model ... >> >> class PurchaseOrder(models.Model): >> po_number = models.IntegerField(unique=True) >> >> >> and a function ... >> >> 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 >> >> >> what I'm trying to do is use the get_po_number() function to get a >> default value for the po_number field in the model. However when I set the >> default in the model like so... >> >> class PurchaseOrder(models.Model): >> po_number = models.IntegerField(unique=True, >> default=get_po_number(self)) >> >> it says "NameError: name 'self' is not defined" >> >> >> What am I doing wrong? Thanks! >> -- >> 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 [email protected]. >> To post to this group, send email to [email protected]. >> 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 >> <https://groups.google.com/d/msgid/django-users/6183c81e-54b8-4779-9bf3-c8c9e734f248%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> 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* <http://www.efa-project.org>, and is believed to be >> clean. >> Click here to report this message as spam. >> <http://lsefa1.linear-systems.com/cgi-bin/learn-msg.cgi?id=A29221028B6.A6CCB&token=d1d88836c3b5a866af3cc86dbf50fb46> >> >> >> -- >> 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 [email protected]. >> To post to this group, send email to [email protected]. >> 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/945d5f35-e98b-b1f1-a527-32ff83687668%40linear-systems.com >> <https://groups.google.com/d/msgid/django-users/945d5f35-e98b-b1f1-a527-32ff83687668%40linear-systems.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> >> >> -- >> 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 >> [email protected]. >> To post to this group, send email to [email protected]. >> 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 >> <https://groups.google.com/d/msgid/django-users/a37859a4-3c35-6f19-6a9b-ef997361bcee%40gmail.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > 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/CAGJrYjaMM7NdLHBXYxtzh5Ysii6qeepVTAGN2k3MHmZhnEmkrw%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAGJrYjaMM7NdLHBXYxtzh5Ysii6qeepVTAGN2k3MHmZhnEmkrw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CAHV4E-dMGdsaZHNjAfYjqBn4LJnRKCO5TNqw34hP%2BAg%3D2KkRfg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

