How to Filter Empty or NULL Fields in Django QuerySet?
Last Updated :
23 Jul, 2025
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:
ID | First Name | Middle Name | Last Name |
---|
1 | Arun | Kumar | Yadav |
2 | Meera | '' | Pathak |
3 | Suraj | NULL | Kumar |
4 | Satyam | '' | 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>]>
Filtering for null valuesHere, 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.
Filtering for Empty Values3. 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.
Filtering for Both Empty or NullConclusion
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.
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. OOPs is a way of organizing code that uses objects and classes to represent real-world entities and their behavior. In OOPs, object has attributes thing th
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read