Reputation: 1551
I'm writing a queryset for data and result needs to be list of objects of that particular user. (logged in user)
This is what I've got so far:
class List(ListView):
def get_queryset(self):
qry = House.objects.filter(user__user_id=self.request.user).all()
return qry
models.py:
from django.contrib.auth.models import User
class House(models.Model):
user_id = models.ForeignKey(User, on_delete=models.CASCADE)
...
...
I suspect that the very filtering condition is wrong because the traceback says no such field user
What can I try to solve this ?
Upvotes: 0
Views: 134
Reputation: 3385
You have a couple of issues here:
qry = House.objects.filter(user__user_id=self.request.user).all()
Is matching a user id, with a User object (i.e. not a user id). What you want is:
qry = House.objects.filter(user=self.request.user).all()
(This in conjunction with the next change)
On Your House
model, you're specifying user_id
. This is confusing because what you are actually mapping to is the User
object. Django will of course use the id
field to do this, but you don't need to specify it. It is more normal to define your mapping to a foreign key as the name of the FK model (only in lower case, obviously, as it's a field name).
user = models.ForeignKey(User, on_delete=models.CASCADE)
Upvotes: 1