Reputation:
When I try to use get() it gives me a multiple objects returned error. 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>}
Upvotes: 1
Views: 1947
Reputation: 2322
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. Your use of filter is using a raw number, your use of get is using the more idiomatic object-as-relation way.
Upvotes: 2
Reputation: 40052
Your get
call in the traceback is not equivalent to the filter
call in your question:
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). Also, without seeing the rest of your code we don't know if user
and request.user
are the same.
If you simply exchange get
for filter
you should end up with the expected result:
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.
Upvotes: 4
Reputation: 42757
If the Score
table has a user
field which is a ForeignKey to the User
table, and you have a local variable user
which is also a User
object then you should do
latest_poll_list = Score.objects.filter(user=user)
Upvotes: 0