1

This is my django views code snippet:

if (calltype == 'save'):
        bill_data = json.loads(request.POST.get('bill_details'))
        Invoice=salesInvoice()
        customerkey = request.POST.get('customer')
        Invoice.customer_key = Customer.object.get(key__iexact=customerkey)
        Invoice.total = request.POST.get('total')
        Invoice.grand_discount = request.POST.get('grand_discount')
        Invoice.amount_paid = request.POST.get('amount_paid')
        #Invoice.save()

        #saving the salesLineItem and linking them with foreign key to invoice
        for data in bill_data:
            LineItem = salesLineItem()
            #LineItem.invoice_no = salesInvoice
            itemcode=data['itemCode']
            LineItem.name = Product.object.get(key__iexact=itemcode).name
            LineItem.unit = Product.object.get(key__iexact=itemcode).unit
            LineItem.batch = Product.object.get(key__iexact=itemcode).batch
            LineItem.discount1 = Product.object.get(key__iexact=itemcode).discount1
            LineItem.discount2 = Product.object.get(key__iexact=itemcode).discount2
            LineItem.quantity = int(data['itemQuantity'])
            LineItem.free = int(data['itemFree'])
            LineItem.mrp = Product.object.get(key__iexact=itemcode).mrp
            LineItem.selling_price = Product.object.get(key__iexact=itemcode).selling_price
            LineItem.vat_type = Product.object.get(key__iexact=itemcode).vat_type
            LineItem.vat_percent = Product.object.get(key__iexact=itemcode).vat_percent
            #LineItem.save()
        #this part is just for checking
        response_data['name'] = Product.object.get(key__iexact=itemcode).name

Do note that I've commented out the lines used to save the invoice and lineitem. Once I un-comment these lines, the error i get is:

name 'Invoice' is not defined

I also overrode the save method of the django model (for the salesInvoice model). Following is its code (in django models file) and in the commented line I've a small explanation of what that does:

#the save method is overriden to give unique invoice ids, slug and customer_name
def save(self, *args, **kwargs):
    today = dt.date.today()
    today_string = today.strftime('%y%m%d')
    next_invoice_number = '01'
    last_invoice = Invoice.objects.filter(invoice_id__startswith=today_string).order_by('invoice_id').last()
    if last_invoice:
        last_invoice_number = int(last_invoice.invoice_id[6:])
        next_invoice_number = '{0:03d}'.format(last_invoice_number + 1)
    self.invoice_id = today_string + next_invoice_number
    if not self.id:
        self.slug=slugify(self.invoice_id)
        customer_name = Customer.object.get(key__iexact=customer_key).name
        address = Customer.object.get(key__iexact=customer_key).address
    super(Invoice, self).save(*args, **kwargs) 

I dont understand where's the error. Any help will be appreciated.

EDIT 1 - Adding the traceback

Traceback:
File "C:\Users\Ganguly PC\Desktop\DjangoStudy\my_env\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request,  *callback_args, **callback_kwargs)
File "C:\Users\Ganguly PC\Desktop\DjangoStudy\billinv\billbrain\views.py" in salesinvoice
  173.          Invoice.save()
File "C:\Users\Ganguly PC\Desktop\DjangoStudy\billinv\billbrain\models.py"   in save
  189.      last_invoice =         Invoice.objects.filter(invoice_id__startswith=today_string).order_by('invoice_id').last()

Exception Type: NameError at /master/salesinvoice/
Exception Value: name 'Invoice' is not defined
Request information:
GET: No GET data

POST:
amount_paid = '0.00'
customer = 'fr'
csrfmiddlewaretoken = 'xxxxxxxxxxxxxxxxxxxxx'
bill_details = '[{"itemCode":"rev","itemQuantity":"5","itemFree":"0"}]'
grand_discount = '0.00'
total = '435.00'
calltype = 'save'
Sayantan
  • 315
  • 5
  • 20
  • You need to learn how to read the stacktrace. Can you edit your question to post the full stacktrace please? – Shang Wang Apr 20 '16 at 18:43
  • Well Shang, sure. I'm editing my question with the stacktrace. But is this not called traceback in HTML-Python (Django) & stacktrace in java/c ?? – Sayantan Apr 20 '16 at 18:48
  • I might be wrong on the terminology, thanks for the correction. But I'm more interested in helping people on their problem. – Shang Wang Apr 20 '16 at 18:51
  • @Shang, thanks a lot :) Really appreciate you helping me out!! I just pointed out to help others - if they see this question - ever !! – Sayantan Apr 20 '16 at 18:57
  • Is the error because of the fact that my databse (of inovices) is blank and the "filter" method to trace the last invoice fails? – Sayantan Apr 20 '16 at 19:03

1 Answers1

1

It's obvious in your traceback. Line 173 calls Invoice.save() then it goes to the save() method you overrode. Then Line 189 shows up and tells you the closest place where the error occurs: You were doing:

last_invoice = Invoice.objects.filter(invoice_id__startswith=today_string).order_by('invoice_id').last()

But Invoice is not defined. It's more of a python question than a django question. You need to have the class name defined first then use it. In views you did import Invoice but here you can't, but you could use type(self) to refer to the instance. So do:

last_invoice = type(self).objects.filter(invoice_id__startswith=today_string).order_by('invoice_id').last()
Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • I suggest you learn some python basics before jumping into django, it will save you a lot of time asking these basic questions in stackoverflow: http://stackoverflow.com/questions/14804084/python-nameerror-name-is-not-defined – Shang Wang Apr 20 '16 at 19:12
  • Thanks a lot Shang!! Well that was a very stupid mistake! Surely I'm learning python alongside (I went through python 101 & 102), but you see I'm a non tech guy planning on a startup and I dont have a lot of time :(. – Sayantan Apr 21 '16 at 03:06