Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

python - Field 'id' expected a number but got 'admin'. in django

i created this to make searchbox to search models, when i replace body instead of author it works perfectly and fetches data accurately but when i use foreign key object string input is not working but when i type pk of user it fetches the username accurately but does not accept string for eg: when i type name of the post it gets but on author when i type name of the author it says following error and does not accept string but can accept pk and fetch author

pls edit this code such that when i type name of the author it gets the name and their post

HELP IS MUCH APPRECIATED

  1. views.py

       def search(request):
           query = request.GET['query']
           allposts = bio.objects.filter(author=query)
           params = {'allposts':allposts}
           return render(request,'profile_app/search.html',params)
    
  2. models.py

     class bio(models.Model):
         author = models.ForeignKey(User,on_delete=models.CASCADE)
         body = models.CharField(max_length=50)
    
  3. search.html

    <h1>this is home</h1>
    <form action="/search" method="get">
    <input type="search" name="query">
    <button type="submit">press here</button>
    </form>
    
question from:https://stackoverflow.com/questions/65857311/field-id-expected-a-number-but-got-admin-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...