Wednesday, July 26, 2017

ValueError: invalid literal for int() with base 10:

Hello, I came across this error:

"ValueError: invalid literal for int() with base 10:" after I made a field in my models.py a ForeignKey. Below is my models.py

from django.conf import settings
from django.db import models
from django.core.urlresolvers import reverse
from django.utils import timezone
import datetime

from contacts.models import Contact
#from django.contrib.auth.models import User

# Create your models here.
class Invoice(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    their_company = models.ForeignKey(Contact, on_delete=models.CASCADE,blank=True,
    null=True,)
    invoice_number = models.CharField(max_length=50, default='')
    bill_to = models.CharField(max_length=255, default='')
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL)
    
    def save(self, *args, **kwargs):
        if not self.id:
            today = datetime.date.today()
            date_str = datetime.datetime.strftime(today, '%y%m%d')
            doc_str = 'ACIN'
            last_invoice = Invoice.objects.filter(invoice_number__startswith=doc_str).order_by('invoice_number').last()
            if last_invoice:
                last_invoice_num = last_invoice.invoice_number[-2:]
                new_invoice_num = int(last_invoice_num) + 1
                new_invoice_num = "%02d" % new_invoice_num
            else:
                new_invoice_num = '01'
            self.invoice_number = doc_str + date_str + new_invoice_num
        super(Invoice, self).save(*args, **kwargs)
        
        
    class Meta:
        ordering = ['invoice_number', ]
        
    def __str__(self):
        return self.invoice_number
        
    def get_absolute_url(self):
        return reverse("accounting:invoices:detail", kwargs={"pk": self.pk})


The 'their_company' field I think is whats giving the error. I think it is because it is trying to convert that field to an int but its not something that can be converted to an int. I'm new to django and I know I have something configured wrong somewhere but not sure where. 

Thanks for your help

--
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/2fe6b89f-e93f-4035-8d8e-9d662832c063%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment