Reputation: 1572
have a model "User" and "Match", this second model is used to always connect 2 Users. I am trying to do a search for a "match" that have the two specific users.
class User(models.Model):
uuid = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False, db_index=True)
email = models.EmailField(_('email address'), blank=False, unique=True, db_index=True)
class Match(models.Model):
uuid = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True)
users = models.ManyToManyField(User, related_name='matches')
The initial idea was to use update_or_create, I discovered that it was not possible. So I went to get_or_create, but now I've tried a simple Filter.
The maximum I got is to repeat the value of the match model (get 2 results with the same value)
Test 1
query = reduce(operator.or_, (Q(users__user__uuid=item) for item in LIST))
result = Match.objects.filter(query)
Count: 0
Test 2
test = Match.objects.filter(users__user__in=LIST))
Count: 0
Test 3
test = Match.objects.filter(users__in=LIST)
Count: 2 (duplicated)
LIST = FUNCTION:
def list_uuid(user1, user2):
return {str(user.uuid), str(user2.uuid)}
Upvotes: 1
Views: 69
Reputation: 30472
Try this:
qs = Match.objects.filter(uuid__in=user1.matches.all(), users=user2)
Upvotes: 1