49👍
✅
If I understand you correctly, the following should do the trick:
favorites = Favorite.objects.filter(user=request.user)
color_ids = favorites.values_list('item__color', flat=True).distinct()
colors = Color.objects.filter(id__in=color_ids)
There has to be a cleaner way than that though.
Edit: A much cleaner solution:
colors = Color.objects.filter(item__favorite__user=request.user).distinct()
8👍
Can you do:
Color.objects.filter(item__favorite__user = request.user).distinct()
You might have to set some related_name
s on your foreign keys if these aren’t the defaults (I can never remember the defaults).
👤Dave
- [Django]-Edit/show Primary Key in Django Admin
- [Django]-OperationalError: database is locked
- [Django]-Django – query filter on manytomany is empty
- [Django]-Count frequency of values in pandas DataFrame column
- [Django]-Django – filtering on foreign key properties
- [Django]-Serving gzipped content from django
0👍
The is values_list(*fields, flat=False, named=False)
method, so run it on your objects, for example:
user.groups.values_list('name', flat=True)
👤Alex
- [Django]-From virtualenv, pip freeze > requirements.txt give TONES of garbage! How to trim it out?
- [Django]-Can i add help text in django model fields
- [Django]-Django migrate –fake and –fake-initial explained
Source:stackexchange.com