[英]Django: Filter() not returning anything
When I try to use get() it gives me a multiple objects returned error. 当我尝试使用get()时,它给了我多个对象返回的错误。 I tried filter and it returns absolutely nothing. 我尝试过滤器,它绝对不会返回任何内容。 Here is my code: 这是我的代码:
latest_poll_list = Score.objects.filter(user=user.id)
It doesn't return anything. 它不返回任何东西。 Hopefully this is just a syntax error; 希望这只是语法错误; not an error that I have to re-write the program. 不是我必须重新编写程序的错误。 Thanks in advance. 提前致谢。
get: 得到:
Environment:
Request Method: GET
Request URL: http://localhost:8000/scores/
Django Version: 1.3
Python Version: 2.5.5
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'es']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/julian/Documents/EpicScore/es/views.py" in scoreindex
30. latest_poll_list = Score.objects.get(user=request.user)
File "/usr/local/lib/python2.5/site-packages/django/db/models/manager.py" in get
132. return self.get_query_set().get(*args, **kwargs)
File "/usr/local/lib/python2.5/site-packages/django/db/models/query.py" in get
351. % (self.model._meta.object_name, num, kwargs))
Exception Type: MultipleObjectsReturned at /scores/
Exception Value: get() returned more than one Score -- it returned 3! Lookup parameters were {'user': <User: jmeyer>}
Your get
call in the traceback is not equivalent to the filter
call in your question: 您在追溯中的get
调用与您问题中的filter
调用不同:
latest_poll_list = Score.objects.get(user=request.user)
!=
latest_poll_list = Score.objects.filter(user=user.id)
In the first you are passing an object, user
as the argument, in the second you're passing the id value (probably an integer). 在第一个中,您传递一个对象( user
作为参数,在第二个中,您传递id值(可能是整数)。 Also, without seeing the rest of your code we don't know if user
and request.user
are the same. 同样,在没有看到其余代码的情况下,我们也不知道user
和request.user
是否相同。
If you simply exchange get
for filter
you should end up with the expected result: 如果您只是简单地将get
替换为filter
,则应该get
预期的结果:
latest_poll_list = Score.objects.filter(user=request.user)
Otherwise you could do: 否则,您可以执行以下操作:
latest_poll_list = Score.objects.filter(user__id=request.user.id)
If you pass in the user
object, the query is internally comparing user.id
against the database column user_id
which is how the data for the ForeignKey
is actually stored. 如果您在通过user
对象,查询内部比较user.id
对数据库列user_id
这对数据如何ForeignKey
实际存储。
You have a corrupted dataset, or maybe your models aren't working like you think they are. 您的数据集已损坏,或者您的模型可能无法正常运行。 Somehow you managed to make it so that the user isn't a unique identifier for the Score. 您设法做到了这一点,以使用户不是分数的唯一标识符。
As for why filter isn't returning anything, maybe it's because you're calling it differently? 至于为什么过滤器不返回任何东西,也许是因为您以不同的方式调用它吗?
filter(user=user.id)
vs. 与
get(user=request.user)
This leads me to believe the user is a remote key that gets automagically handled by django, rather than a raw number. 这使我相信用户是由django自动处理的远程密钥,而不是原始数字。 Your use of filter is using a raw number, your use of get is using the more idiomatic object-as-relation way. 您对filter的使用使用的是原始数字,对get的使用使用的是更惯用的对象关联关系方式。
如果Score
表的user
字段是User
表的ForeignKey,并且您有一个本地变量user
也是User
对象,那么您应该
latest_poll_list = Score.objects.filter(user=user)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.