qliq
qliq

Reputation: 11807

Django: invalid literal for int() with base 10

I am new to Django and trying to pass an author's name to a view and filter out quote objects based on the author's name. here are the codes:

models.py

class Author(models.Model):
    author_name = models.CharField(max_length=50, default='unknown')
    author_info = models.TextField(max_length=1000)


class Quote(models.Model):
    author = models.ForeignKey(Author)
    quote = models.TextField(max_length=500)
    category= models.ForeignKey(Category)
    pub_date = models.DateTimeField('date published')

urls.py:

url(r'^quotes/(?P<name>\w+)/$', 'quotes.views.quotesbyauthor'),

views.py

def quotesbyauthor(request, name):
    aquotelist = Quote.objects.filter(author__exact = name)
    return render_to_response(quotes_by_author.html, {'aquotelist': aquotelist })

However I get this error when I try to get http://127.0.0.1:8000/quotes/you/ ('you' is a test author object, already created)

ValueError at /quotes/you/

invalid literal for int() with base 10: 'you'

Request Method:     GET
Request URL:    http://127.0.0.1:8000/quotes/you/
Django Version:     1.3.1
Exception Type:     ValueError
Exception Value:    

invalid literal for int() with base 10: 'you'

Exception Location:     /home/qliq/djenv/lib/python2.6/site-packages/django/db/models/fields/__init__.py in get_prep_value, line 479
Python Executable:  /home/qliq/djenv/bin/python
Python Version:     2.6.6
Python Path:    

['/home/qliq/djenv/quoteapp',
 '/home/qliq/djenv/lib/python2.6/site-packages/distribute-0.6.10-py2.6.egg',
 '/home/qliq/djenv/lib/python2.6/site-packages/pip-0.7.2-py2.6.egg',
 '/home/qliq/djenv/lib/python2.6',
 '/home/qliq/djenv/lib/python2.6/plat-linux2',
 '/home/qliq/djenv/lib/python2.6/lib-tk',
 '/home/qliq/djenv/lib/python2.6/lib-old',
 '/home/qliq/djenv/lib/python2.6/lib-dynload',
 '/usr/lib/python2.6',
 '/usr/lib/python2.6/plat-linux2',
 '/usr/lib/python2.6/lib-tk',
 '/home/qliq/djenv/lib/python2.6/site-packages']

I appreciate your help to resolve this.

Upvotes: 39

Views: 80121

Answers (6)

joe-khoa
joe-khoa

Reputation: 610

I have faced and solved similar issue, focus on attribute : author = models.ForeignKey(Author) django expect that you pass an Instance of Author class to the author field. When you search django want to get a id (a integer number of this Instance): so to get :

aquotelist = Quote.objects.filter(author__exact = name)

you have to start by :

  1. create obj_Author = Author.objects.get(author_name = name)

  2. aquotelist = Quote.objects.get(author__exact = obj_Author.id)

PS: ( I used get() in my example but if you expect to get multiple record, you can you use filter() and modify something. but main point is that because you work with a "ForeignKey field" so you need to give it a id like obj_Author.id. )

Upvotes: 1

Omar Diaa
Omar Diaa

Reputation: 1

I faced this issue and I figured out that I made records in the DB table that in your case is (Quote) before making the foreign key relationship with (Author) then after making the foreign key relationship Django doesn't know what to do to handle the old data in Quote model (to which records in Author should old records be assigned to)

--> The simplest solution is to:

delete your old Quote records: Quote.objects.all().delete inside your shell

recreate and reassign its new records correctly

makemigrations, migrate and runserver ... done

Upvotes: 0

Rishabh verma
Rishabh verma

Reputation: 1

This will fix your problem permanently ::go to your directory

C:\python 37\Lib\site-packages\django\db\models\fields 

and edit the file __init__.py and edit line 1807; replace

return int(value)

with

return int()

So just delete the argument value then your program will except all field references.

Upvotes: -7

hassanzadeh.sd
hassanzadeh.sd

Reputation: 3461

I think you should reset your migrate , you can see this link :

https://stackoverflow.com/a/54768217/9533909

i hope that help you.

Upvotes: 0

Alasdair
Alasdair

Reputation: 308779

You want to search on the author's author_name field, not the id.

Quote.objects.filter(author__author_name=name)

With your current search, author__exact, Django expects name to be the id of the author, so gives an error because you is not an integer.

Upvotes: 46

U-DON
U-DON

Reputation: 2140

aquotelist = Quote.objects.filter(author__author_name__exact = name)

Try changing the corresponding line to the above. The way you have it now, you are matching author to the given name, but author is probably considered by its ID here, definitely not by its author_name. The format is as follows:

Quote.objects.filter([model]__[field]__exact = [whatever])

Upvotes: 5

Related Questions