[Django]-How to filter empty value in a Django queryset?

5👍

If you want to display all the emails that are not empty (filter-out empty) then you can exclude them using

users = User.objects.exclude(email__isnull=True)

If you want to get all the columns with empty email value then use

users = User.objects.filter(email__isnull=True)

1👍

Declare model:

class User(models.Model):
    email = models.EmailField(null=True)

then:

users = User.objects.filter(email__isnull=True)

If you want to exclude empty value then:

users = User.objects.exclude(email__isnull=True)

Leave a comment