class OrganizationUser(models.Model):
pass
class Feedback(models.Model):
teams_shared_with = models.ManyToManyField('team.Team', related_name="feedback")
class Team(models.Model):
leads = models.ManyToManyField('organizations.OrganizationUser', related_name='teams_lead_at')
members = models.ManyToManyField('organizations.OrganizationUser', related_name='teams_member_at', blank=True)
I have the above models, all the fields were not shown. I want to filter those feedback which are shared in any team where I am a member or an admin.
I would like to do something like:
Feedback.objects.filter(teams_shared_with is a subset of organization_user.teams_lead_at.all()|organization_user.teams_member_at.all())
How would I do that?
CodePudding user response:
from django.db.models import Q
user1 = OrganizationUser.objects.first() #some organization user
Feedback.objects.filter(
Q(teams_shared_with__leads=user1)
|Q(teams_shared_with__members=user1)
)
CodePudding user response:
There is chance of duplication of Feedback use distinct() to avoid that
from django.db.models import Q
users = OrganizationUser.objects.all()
Feedback.objects.filter(
Q(teams_shared_with__leads = users)
|Q(teams_shared_with__members = users)
).distinct()