3👍
✅
You can use .distinct
and values_list
to do this beautifully.
rooms = Room.objects.filter(users__in=[user_id_1, user_id_2])\
.distinct("room_id").values_list("room_id", flat=True)
1👍
Use distinct(<field name>)
method
rooms = Room.objects.filter(users__in=[user_id_1, user_id_2]).distinct('users')
- [Django]-Displaying a message as a popup/alert in Django?
- [Django]-Arguments disappear from a dictionary when passed to a function
- [Django]-Django case when else in filter
- [Django]-How to get something to appear on every page in Django?
Source:stackexchange.com