Python Django QuerySet Filter IN Operator Example

Published On: 24/08/2022 | Category: Django


Hi Dev,

In this tutorial we will go over the demonstration of python django queryset filter in operator example. I explained simply about python django filter in example. this example will help you python django filter in. you will learn django queryset filter in condition example.

So, let’s understand the use of the “in” field lookup with the help of an example. For this, we will use the same Employee model.

You can use these examples with django3 (Django 3) version.

let's see bellow example here you will learn python Django filter distinct example.

Django Admin Interface:



Example

In this example we will use the same Employee model. to get the Get employees entries with ids.

views.py
from django.http import HttpResponse
from core.models import Employee

def index(request):
    queryset = Employee.objects.filter(pk__in=[2,3]).values()
    print(queryset)

    return HttpResponse(queryset)
Output
[
    {
        'id': 2, 
        'firstname': 'Vishal', 
        'lastname': 'Patel'
    }, 
    {
        'id': 3, 
        'firstname': 'Nikhil', 
        'lastname': 'Patel'
    }
]

I Hope It will help you....