Open In App

How to Filter Empty or NULL Fields in Django QuerySet?

Last Updated : 02 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with a Django project, we often need to filter records where certain fields are either empty or NULL. This is a common requirement, especially when dealing with user-generated data where fields might be left blank. In Django, we can filter a QuerySet where a specific field has no value or is explicitly set to NULL. In this article, we will explain how to filter such fields in a Django QuerySet, with examples.

NULL Values: In a database, a NULL value signifies the absence of a value. In Django, we can allow a field to store NULL by setting null=True in the model definition.

Empty Fields: An empty field, represented as an empty string (''), contains a value, but that value is a string with zero characters.

Filtering Record based on Empty or NULL Fields in Django

Before we start filtering let’s consider a Django model Person with three fields: first_name, middle_name, and last_name. The middle_name field can be NULL or an empty string:

Python
from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=100)
    middle_name = models.CharField(max_length=100, null=True, blank=True)
    last_name = models.CharField(max_length=100)
    
    def __str__(self) -> str:
        return f'{self.first_name}_{self.middle_name}_{self.last_name}'

Assume we have the following Person records:

IDFirst NameMiddle NameLast Name
1ArunKumarYadav
2Meera''Pathak
3SurajNULLKumar
4Satyam''Singh

Add data to the database:

>>> from myapp.models import Person
>>> Person.objects.create(first_name="Arun", middle_name="Kumar", last_name="Yadav")
<Person: Person object (1)>
>>> Person.objects.create(first_name="Meera", middle_name="", last_name="Pathak")
<Person: Person object (2)>
>>> Person.objects.create(first_name="Suraj", middle_name=None, last_name="Kumar")
<Person: Person object (3)>
>>> Person.objects.create(first_name="Satyam", middle_name="", last_name="Singh")
<Person: Person object (4)>
>>>

Now let's start filtering records.

1. Filtering for NULL Values

To filter records where the middle_name field is NULL, we can use the __isnull lookup operation.

For Example: This will return all Person objects where middle_name is NULL.

null_middle_names = Person.objects.filter(middle_name__isnull=True)

Output

<QuerySet [<Person: Suraj_None_Kumar>]>
Screenshot-2024-09-02-114221
Filtering for null values

Here, Suraj is the only person whose middle_name is None.

2. Filtering for Empty values:

To filter records where the middle_name field is an empty string (''), we can use a simple equality check (middle_name=''). Or we can also use iexact and exact lookups to carry out the same operation.

Example: This will return all Person objects where middle_name is an empty string.

empty_middle_names = Person.objects.filter(middle_name='')
or
empty_middle_names = Person.objects.filter(middle__exact='')
or
empty_middle_names = Person.objects.filter(middle__iexact='')

Output

<QuerySet [<Person: Meera__Pathak>, <Person: Satyam__Singh>]>

Here, Satyam and Meera has middle_name=''. Also, all three approach gives the same result.

Screenshot-2024-09-02-114545
Filtering for Empty Values

3. Filtering for Both NULL and Empty Value

If we want to filter records where the middle_name field is either NULL or an empty string, we can combine two filters using Q objects and the | (OR) operator:

For Example: This query will return all Person objects where the middle_name field is either NULL or an empty string.

null_or_empty_middle_names = Person.objects.filter(middle_name__isnull=True) | Person.objects.filter(middle_name='')
or
null_or_empty_middle_names = Person.objects.filter(Q(middle_name__isnull=True) | Q(middle_name=''))

Output:

<QuerySet [<Person: Meera__Pathak>, <Person: Suraj_None_Kumar>, <Person: Satyam__Singh>]>

Here, Satyam, Meera and Suraj has middle_name withe EMPTY or NULL.

Screenshot-2024-09-02-114943
Filtering for Both Empty or Null

Conclusion

Filtering for NULL or empty fields in Django is a simple process using the Django ORM. Whether we need to check for missing data, validate form submissions, or perform data migrations, knowing how to filter these fields is crucial. By understanding and applying the isnull lookup, equality checks, and Q object combinations, we can efficiently manage our data and ensure our queries return exactly what you need.


Next Article
Article Tags :
Practice Tags :

Similar Reads