1👍
You can simply work with the .group_id
of the user
:
user = User.objects.get(id=user_id)
User.objects.filter(group_uuid=user.group_uuid)
Or if you need one query, you can work with .filter(…)
instead of .get(…)
for the user
:
user_uuids = User.objects.filter(id=user_id).values('group_uuid')
User.objects.filter(group_uuid__in=user_uuids)
Since QuerySet
s are lazy, it will not perform the user_uuids
query, but simply encode this as a subquery.
That being said, using subqueries are not always more efficient. Especially MySQL is known to recalculate the subquery per element, even if the subquery is "constant" with respect to that item.
Source:stackexchange.com