Correct way to create a Q object that filters all records in a Django QuerySet?

Now I use only Q(id=0), and it depends on the DB. Or maybe Q(pk__isnull=True)better? This is useful for concatenating Q objects using an operator |.

+4
source share
2 answers

In fact, there is a special method in Django QuerySet. Model.objects.none()always returns an empty request.

0
source

Q(pk__isnull=True)better because it PRIMARY KEYcannot contain values NULL. It is possible that some instance may have id=0.

+3
source

All Articles