Exclude one or multiple objects from Django Queryset Example

Hi Dev,
This simple article demonstrates of exclude one or multiple objects from django queryset example. This article goes in detailed on exclude one or multiple objects from django queryset. I would like to show you django exclude queryset from queryset. We will look at example of django exclude multiple conditions. follow bellow step for django exclude column from queryset.
Django exclude() method filter the objects that do not match the given lookup parameters and return a new queryset.
You can use these examples with django3 (Django 3) version.
let's see bellow example here you will learn exclude one or multiple objects from django queryset example.
Create a ModelIn this step we will require the database model for storing data from employee.Open the models.py file and add the following code:
models.pyfrom django.db import models # Create your models here. class Employee(models.Model): firstname = models.CharField(max_length=255) lastname = models.CharField(max_length=255)
Django Admin Interface:

Example 1 : Exclude one object from Queryset views.py
from django.shortcuts import render from django.http import HttpResponse from core.models import Employee def exclude(request): employees = Employee.objects.exclude(firstname="Vishal").values() #print QuerySet print(employees) return HttpResponse(employees)Output
[ { 'id': 1, 'firstname': 'Bhavesh', 'lastname': 'Sonagra' } ]Example 2 : Exclude multiple objects from Queryset
In this second example to exclude multiple objects, we'll use in filter with the exclude() method.
views.pyfrom django.shortcuts import render from django.http import HttpResponse from core.models import Employee def exclude(request): excludes = ['Nikhil', 'Bhavesh'] employees = Employee.objects.exclude(firstname__in=excludes).values() #print QuerySet print(employees) return HttpResponse(employees)Output
[ { 'id': 2, 'firstname': 'Vishal', 'lastname': 'Patel' } ]
I Hope It will help you....