Python Django Queryset Filtering Last Updated : 22 May, 2025 Comments Improve Suggest changes Like Article Like Report In Django, QuerySet filtering allows you to retrieve only the data you need from the database. QuerySets help you filter, sort and organize your data efficiently, making it easy to interact with your models.This article will explore how to filter datasets using Django’s filter(), exclude() and advanced filtering with Q objects, let's create a model named "user".Define the User ModelIn projectApp/models.py, define a User model with fields like user_name, city and country: Python from django.db import models class User(models.Model): user_name = models.CharField(max_length=20) city = models.CharField(max_length=20, blank=True, null=True) country = models.CharField(max_length=20, blank=True, null=True) def __str__(self): return self.user_name Register the Model in Django AdminTo make the User model accessible in the Django Admin interface, register it in projectApp/admin.py: Python from django.contrib import admin from .models import User admin.site.register(User) Create a superuser to access the model through admin panel.python manage.py createsuperuserSuppose we have the following the entries in our User model:Snapshot of the databaseQuerySet Filtering ExamplesNow that we have our User model, let’s explore how to filter data using QuerySets.1. Filtering All Users:To retrieve all users from a specific country, use filter():users = User.objects.filter(country='India')This retrieves all users where the country is 'India':Snapshot of the command in shell2. Excluding Specific Records:To exclude records that match a condition, use exclude():users = User.objects.filter(country='India').exclude(city='Mumbai')This retrieves all users from India, excluding those from Mumbai.Snapshot of shell3. Using Q Objects for Advanced Filtering:Q objects allow for more complex queries, like OR conditions:from django.db.models import Qusers = User.objects.filter(Q(country='India') | Q(city='New York'))This returns users who are from either India or New York.Snapshot of shell Comment More infoAdvertise with us Next Article Python Django Queryset Filtering kunalp143 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2022 Practice Tags : python Similar Reads Django URL patterns | Python In Django, views are Python functions that handle HTTP requests. These views process the request and return an HTTP response or an error (e.g., 404 if not found). Each view must be mapped to a specific URL pattern. This mapping is managed through URLConf (URL Configuration).In this article, we'll ex 3 min read Views In Django | Python Django Views are one of the vital participants of the MVT Structure of Django. As per Django Documentation, A view function is a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, a redirect, a 404 error, an XML document, an ima 6 min read Django Query Set - get A QuerySet is a collection of data retrieved from the database. You can think of it as a list of objects. QuerySets make it easier to get only the data you need by letting you filter, sort, and organize your data early on before actually fetching it from the database.In this article, we will learn h 3 min read How to Perform Query Filtering in Django Templates Sometimes we may want to filter or modify the list of objects within the template itself to tailor the data being displayed. While filtering should generally be done at the view level for clarity and separation of concerns, Django templates provide some basic filtering capabilities through template 5 min read Count() vs len() on a Django QuerySet In Django, when working with database query sets, developers often need to determine the number of records that meet certain criteria. Django offers two primary ways to accomplish this: using the count() method on a QuerySet, or the Python built-in len() function. Each method has its specific use ca 3 min read How to perform OR, AND and NOT in Django QuerySet Filtering We can use the Q objects in django.db.models to create an OR, AND, and NOT filter in a Django query. The Q objects are used to form complex filtering by joining several conditions through logic operators like OR, AND, and NOT. In this article, we will learn to perform AND, OR, and NOT filters while 4 min read Django Query Set - Order By order_by() method in Django QuerySets is used to sort query results based on one or more fields, either in ascending or descending order. This helps display data sorted by criteria like salary, name, date, etc., directly from the database query.In this article we will learn all about order_by method 2 min read Python MongoDB - Query MongoDB is a cross-platform document-oriented and a non relational (i.e NoSQL) database program. It is an open-source document database, that stores the data in the form of key-value pairs. What is a MongoDB Query? MongoDB query is used to specify the selection filter using query operators while ret 3 min read Django Template Filters Django Template Engine provides filters which are used to transform the values of variables and tag arguments. We have already discussed major Django Template Tags. Tags can't modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one's own nee 6 min read Intermediate fields in Django | Python Prerequisite: Django models, Relational fields in DjangoIn Django, a many-to-many relationship is used when instances of one model can be associated with multiple instances of another model and vice versa. For example, in a shop management system:A Customer can purchase multiple Items.An Item can be 2 min read Like