Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 302 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to filter model results for multiple values for a many to many field in django

#1
I have the following Model:

class Group(models.Model):
member = models.ManyToManyField(Player, through='GroupMember')
name = models.CharField(max_length=20, unique=True)
join_password = models.CharField(max_length=20)
date_created = datetime.datetime.now()

def __unicode__(self):
return str(self.name)

class GroupMember(models.Model):
member = models.ForeignKey(Player)
group = models.ForeignKey(Group)
rating = models.IntegerField(default=1500)
played = models.IntegerField(default=0)
wins = models.IntegerField(default=0)
losses = models.IntegerField(default=0)
experience = models.IntegerField(default=0)
admin = models.BooleanField(default=0)

As you can see the group is made up of members who are players. What I would like to do is given two players I would like to be able to filter the groups that contain both of these players but I am unsure how to do this type of query.
Reply

#2
The easiest solution for you will be:

p1 = Player.objects.get(id=1)
p2 = Player.objects.get(id=2)
groups = Group.objects.filter(member=p1).filter(member=p2)

Note that you can't use the __in filter like this because this will result in an OR and return groups that don't contain both players:

Group.objects.filter(member__in=[1, 2])
Reply

#3
If your `Player` model looks like this:

class Player(models.Model):
name = models.CharField(max_length=200)

Then, you can execute this query:

Group.objects.filter(player__name__in=['Player1','Player2'])

Which roughly translates to _"find all groups that have players whose names match 'Player1' and 'Player2'"_

Or you can fetch the `player` objects individually:

p1 = Player.objects.get(name='Player1')
p2 = Player.objects.get(name='Player2')
groups = Group.objects.filter(player=p1).filter(player=p2)



Reply

#4
For me `__in` did not work. I ended up using the [complex Q lookup][1]
which works perfectly and you can `or` filter conditions with. Use it like this:

from django.db.models import Q

p1 = Player.objects.get(name='Player1')
p2 = Player.objects.get(name='Player2')
querySet = Group.objects.filter(Q(member=p1) | Q(member=p2))

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through