'Django exclude users with same email

I have 2 user models in my django project. So it set up like this: UserModel1 has 500 users with email and UserModel2 has 2000 users with email. And 300 users are both registered inn UserModel1 and UserModel2. So that means the two models share 300 users. UserModel1 has 200 excluding the users they both have in common, and UserModel2 has 1700 users exluded the users they have in common.

I wanted to make a query that gets all the useres from UserModel2 that are not registered in UserModel1. So i want to get the 1700 useres they dont have in common. After som research i landed on this:

from django.db.models import Q

not_matching = UserModel2.objects.filter(~Q((email__in=UserModel1.objects.values('email')))

but no luck, any ideas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source