Django: invalid literal for int() with base 10

后端 未结 6 764
时光说笑
时光说笑 2020-12-15 03:14

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

<         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 03:36

    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. )

提交回复
热议问题