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

1,073 views
Skip to first unread message

Alexander Joseph

unread,
Jul 23, 2018, 4:39:06 PM7/23/18
to Django users
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!

Michael MacIntosh

unread,
Jul 23, 2018, 7:55:08 PM7/23/18
to django...@googlegroups.com

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.

Gerald Brown

unread,
Jul 23, 2018, 8:36:14 PM7/23/18
to django...@googlegroups.com

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.

Alexander Joseph

unread,
Jul 23, 2018, 8:55:30 PM7/23/18
to django...@googlegroups.com
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

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.

mottaz hejaze

unread,
Jul 24, 2018, 12:32:59 AM7/24/18
to django...@googlegroups.com
you can use count() in views or models to count number of objects then add the new record

Alexander Joseph

unread,
Jul 24, 2018, 10:08:20 PM7/24/18
to django...@googlegroups.com
For future reference - I had to change my function to

def get_po_number():
    last_po = PurchaseOrder.objects.order_by('po_number').last()
    if last_po:
        last_po_num = last_po.po_number
        new_po_num = last_po_num + 1
    else:
        new_po_num = '1'
    po_number = new_po_num
    return po_number

class PurchaseOrder(models.Model):
    po_number = models.IntegerField(unique=True, default=get_po_number)

And the default value should be set to get_po_number

Reply all
Reply to author
Forward
0 new messages