I got the same error below:
SyntaxError: keyword argument repeated: post__contains
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 Blog
from django.http import HttpResponse
def test(request):
# Here
qs = Blog.objects.filter(
post__contains="popular", post__contains="simple"
) # ↑ ↑ ↑ Here ↑ ↑ ↑ # ↑ ↑ ↑ Here ↑ ↑ ↑
print(qs)
return HttpResponse("Test")
So, I used &
or Q() and &
to run AND
operator with filter()
as shown below:
# "store/views.py"
from .models import Blog
from django.db.models import Q
from django.http import HttpResponse
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, the error above was solved:
<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