Title: Django absolute __in query
Tags: django
Question: I have a list of UUIDs that represent users in a DB table. I want to fetch all the users that match this criteria so I use the ```__in``` filter:

```users = User.objects.filter(user__in=uuids)
```

I would like to raise an exception if not all of the UUIDs appear in the table. In other words, I expect to get a result back for each uuid such that ```len(users) == len(uuids)```. Is there an easy Django way to do it? If not, is there an easy way for me to create this behavior?

Comment: Why not perform this check (with for example `assert len(users) == len(uuids)`?

Comment: @WillemVanOnsem - That's what I'm doing, but I'd prefer to have the code look a bit more elegant as this repeats itself so many times.


Here is another answer: You could use exclude if you like it gives you the objects that does not match the query 

```#this returns true if one or more and dont need to use len()
#if it looks better for you you can try it out
#if everything is fine will return None
if User.objects.filter().exclude(user__in=uiids).first(): 
   raise your_error
```


Here is another answer: Don't think too complicated. You want to know if a list of something is fully contained in another list of something, that's just plain python:

```user_ids = User.objects.values_list('user_id',flat=True)
if not set(uuids).issubset(user_ids): # or use .difference() to get the uuids missing from user_ids
    raise stuff
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:07/18/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<django>', 'Title': 'Django absolute __in query', 'ViewCount': 36, 'id': 51397719, 'user_AboutMe': '', 'user_AccountId': 2381622.0, 'user_DisplayName': 'Avi', 'user_Reputation': 20492.0, 'user_Views': 1556.0, 'user_id': 2083523.0}}