[Django]-Check if each value within list is present in the given Django Model Table in a SINGLE query

7👍

✅

If you know you’re passing in N pks, then a count() query filtered by those pks should have exactly N results.

def do_exist(model, pks):
    return model.objects.filter(pk__in=pks).count() == len(pks)

1👍

qs = MyModel.objects.filter(id__in=pks)

This gives you a queryset that you can apply .all() etc to

0👍

res = MyModel.objects.filter(id__in=pks)

Leave a comment