46đź‘Ť
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.
5đź‘Ť
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])
- [Django]-How do I execute raw SQL in a django migration
- [Django]-Django query datetime for objects older than 5 hours
- [Django]-Django: allow line break from textarea input
1đź‘Ť
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 :
-
create
obj_Author = Author.objects.get(author_name = name)
-
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
. )
- [Django]-How to change Django Admin Custom list field label?
- [Django]-Many-To-Many Fields View on Django Admin
- [Django]-Django – iterate number in for loop of a template
0đź‘Ť
I think you should reset your migrate , you can see this link :
https://stackoverflow.com/a/54768217/9533909
i hope that help you.
- [Django]-Django-tables2: How to use accessor to bring in foreign columns?
- [Django]-Pycharm: set environment variable for run manage.py Task
- [Django]-ForeignKey to abstract class (generic relations)
0đź‘Ť
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
- [Django]-How can one change the type of a Django model field from CharField to ForeignKey?
- [Django]-Is it possible to store an array in Django model?
- [Django]-Cannot import name _uuid_generate_random in heroku django
-7đź‘Ť
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.
- [Django]-What does 'many = True' do in Django Rest FrameWork?
- [Django]-Django – TypeError – save() got an unexpected keyword argument 'force_insert'
- [Django]-Django Setup Default Logging