django - All groups list with additional boolean field showing user membership -
i list of available groups additional boolean field showing if particular user member of group.
#this give me groups related user "myuser" mygroup = myuser.groups.all()
the result list of id, groupname related user "myuser".
how can have list of groups (including not part of mygroup) evidence of membership "myuser" on additional boolean field (user_membership)?
id, groupname, user_membership(true/false)
you use a conditional expression:
group.objects.annotate(user_membership=case( when(user=myuser, then=value(true)), default=value(false), output_field=booleanfield(), ))
this return groups additional user_membership
field containing true
or false
values.
Comments
Post a Comment