How to run "AND" operator with "filter()" without "SyntaxError: keyword argument repeated:" error in Django?

I have Blog model below. *I use Django 3.2.16 and PostgreSQL:

# "store/models.py"

from django.db import models

class Blog(models.Model):
    post = models.TextField()
    
    def __str__(self):
        return self.post

Then, store_blog table has 2 rows below:

store_blog table:

id post
1 Python is popular and simple.
2 Java is popular and complex.

Then, when writing the filter() code with 2 post__contains arguments in test() view to run AND operator as shown below:

# "store/views.py"

from .models import Person

def test(request):
    # Here
    qs = Blog.objects.filter(
             post__contains="popular", post__contains="simple"
         )
    print(qs)

    return HttpResponse("Test")

I got the error below:

SyntaxError: keyword argument repeated: post__contains

So, how to run AND operator with filter() without SyntaxError: keyword argument repeated: error in Django?

You can run AND operator with filter() using & or using Q() and & as shown below:

# "store/views.py"

from .models import Person
from django.db.models import Q

def test(request):

    # With "&"
                                                     # ↓ Here
    qs = Blog.objects.filter(post__contains="popular") & \
         Blog.objects.filter(post__contains="simple")
    print(qs)

    # With "Q()" and "&" 
                           # ↓ Here                    # ↓ Here
    qs = Blog.objects.filter(Q(post__contains="popular") & 
                             Q(post__contains="simple"))
    print(qs)              # ↑ Here

    return HttpResponse("Test")

Then, you can get the result below:

<QuerySet [<Blog: Python is popular and simple.>]> # With "&"
<QuerySet [<Blog: Python is popular and simple.>]> # With "Q()" and "&"
[22/Dec/2022 12:08:22] "GET /store/test/ HTTP/1.1" 200 9

And, there are AND operators according to the query logs of PostgreSQL as shown below and you can check On PostgreSQL, how to log queries with transaction queries such as "BEGIN" and "COMMIT"

enter image description here

In addition, you can run OR operator with filter() using | or using Q() and | as shown below:

# "store/views.py"

from .models import Person
from django.db.models import Q

def test(request):

    # With "&"
                                                     # ↓ Here
    qs = Blog.objects.filter(post__contains="popular") | \
         Blog.objects.filter(post__contains="simple")
    print(qs)

    # With "Q()" and "&" 
                           # ↓ Here                    # ↓ Here     
    qs = Blog.objects.filter(Q(post__contains="popular") | 
                             Q(post__contains="simple"))
    print(qs)              # ↑ Here
                       
    return HttpResponse("Test")

Then, you can get the result below:

<QuerySet [<Blog: Python is popular and simple.>, <Blog: Java is popular and complex.>]>
<QuerySet [<Blog: Python is popular and simple.>, <Blog: Java is popular and complex.>]>
[22/Dec/2022 12:56:41] "GET /store/call_test/ HTTP/1.1" 200 9

And, there are OR operators according to the query logs of PostgreSQL as shown below:

enter image description here

Back to Top