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]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[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.
For more options, visit https://groups.google.com/d/optout.