Reputation: 2500
This is my model:
class Film(models.Model):
filmmaker = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length=250)
In my view a user can select a film from a dropdown and go over the details of that film.
My View:
@login_required()
def home(request):
movies = Film.objects.filter(filmmaker=request.user)
# I added the following to debug. Assume there are 4 elements in the queryset
print movies[0].title, movies[3].title
print movies[1].title, movies[2].title
...
The console output is such and WRONG:
title1 title1
title2 title3
However when I run manage.py shell and create the queryset manually,
>>> movies = Film.objects.filter(filmmaker__email='[email protected]')
>>> print movies[0].title, movies[3].title
>>> print movies[1].title, movies[2].title
The output is as expected:
title1 title4
title2 title3
What is happening here? Where am I going wrong? What can I do to fix it? Can't seem to be able to understand this strange behavior
Upvotes: 0
Views: 95
Reputation: 18835
I don't think I can help you much without seeing the overall database structure and data.
but one thing to note is that in the first instance you query based on request object, and the second you query based on email address.
Maybe before querying, try to elaborate request.user object first, and see whether name/email/etc is what you are expecting.
Upvotes: 2