Django Filter Query Example Tutorial

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

Hi Dev,

Today our leading topic is django query where condition. This article goes in detailed on python django filter with and operator. you will learn django filter example. I explained simply step by step django filter multiple conditions. follow bellow step for django query filter.

So, we will discuss how to use the filter queryset method in Django.

Here i explained simply step by step django filter queryset example:

Employee Table: Example : 1

So, in this step let’s start our shell now. we need to open the command prompt and run the following command.

python manage.py shell

Now, we will execute the following example.

>>> from myApp.models import Employee                       
>>>
>>> q = Employee.objects.filter(first_name='bhavesh').values()
>>>
>>> print(q)
Output
[
    {
        'id': 1,
        'firstname': 'Bhavesh',
        'lastname': 'Sonagra'
    }
]
Example : 2

So, in this second example So, we will discuss how to use the AND operator while using the filter method in Django.

python manage.py shell

Now, we will execute the following example.

>>> from myApp.models import Employee                       
>>>
>>> q = Employee.objects.filter(firstname__startswith='V', lastname__startswith='P').values()
>>>
>>> print(q)
Output
[
    {
        'id': 2,
        'firstname': 'Vishal',
        'lastname': 'Patel'
    }
]

I Hope It will help you....