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

1,636 views
Skip to first unread message

Alexander Joseph

unread,
Jul 26, 2017, 2:52:57 PM7/26/17
to Django users
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

Tim Chase

unread,
Jul 26, 2017, 3:27:28 PM7/26/17
to django...@googlegroups.com
On 2017-07-26 11:52, Alexander Joseph wrote:
> "*ValueError: invalid literal for int() with base 10:*" after I
> made a field in my models.py a ForeignKey. Below is my models.py

The problem lies in this line:

> new_invoice_num = int(last_invoice_num) + 1

For some reason last_invoice_num is an invalid value to pass to
int(). You'd have to check what that actual value is that you're
passing to the function. The error message *should* specify what
that value is:

>>> int("x")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'x'
>>> int("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

but you seem to have truncated it so it's a little hard to tell from
the error message. It's not None as that produces a different
message:

>>> int(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: int() argument must be a string, a bytes-like object or a
number, not 'NoneType'

-tkc



Alexander Joseph

unread,
Jul 26, 2017, 3:42:26 PM7/26/17
to Django users, django...@tim.thechases.com
Hi, thanks for the reply, however when I take out the 'their_company' field altogether everything works fine. Also in the error it references the their_company field. The entire error is  "invalid literal for int() with base 10: 'NewCor'" NewCor is a company in the contacts table. Thanks

Alexander Joseph

unread,
Jul 26, 2017, 5:45:49 PM7/26/17
to Django users, django...@tim.thechases.com
The problem was I had records in the database from before I changed the 'their_company' field to a ForeignKey. Before it was a CharField so some of the records had actual company names in them. I deleted those records since there wasnt any real data in there anyways, just dummy data. Everything works the way its supposed to now.
Reply all
Reply to author
Forward
0 new messages