6👍
✅
Replace:
items = Item.objects.filter(user_obj.items)
With:
user_items = Items.objects.filter(profile=user_obj)
items = Items.objects.exclude(pk__in=user_items)
user_items
will contain a queryset with all Item
objects related to the User
in user_obj
.
items
will contain a queryset with all Item
objects, excluding the ones that are related to the User
in user_obj
You were getting your error because filter()
needs a field from a model:
filter(user='some user')
More information about how to make queries can be read in the docs
1👍
try using:
from django.shortcuts import get_list_or_404
items = get_list_or_404(Item, user=user_obj)
if that doesn’t work, trying uing,
from django.shortcuts import get_object_or_404
items = get_object_or_404(Item, user=user_obj)
👤Jay
- [Django]-Urls.py redirect with URL reversal and parameters — is there any easier way?
- [Django]-Error running manage.py syncdb for first time on Heroku: "You need to specify NAME in your Django settings file"
- [Django]-Don't allow empty string on model attribute
- [Django]-How to rewrite base url in django to add logged in username in the url of all pages instead of app name?
Source:stackexchange.com