Monday, July 23, 2018

How to automatically fill out an auto increment number in form field?

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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

No comments:

Post a Comment