S
S
Sergey2021-01-09 18:57:10
Django
Sergey, 2021-01-09 18:57:10

How to do filter merging in Django?

How to combine multiple filters in django?
If you just write it by hand, it looks like this:
bbs = Bd.objects.filter(par1=pk1).filter(par2=pk2)
Do the following:
filter1 and filter2 should be filled if pk1 and pk2 != 0
I.e. if pk2 !=0 then:
bbs = Bd.objects.filter(par1=pk1)
If pk1!=0 then:
bbs = Bd.objects.filter(par2=pk2)

I did this:

if pk1!= 0:
        fp = '.filter(prepod='
        filter_par1 = f'.filter(par1={pk1})'
        bbs = f'Bd.objects.filter(par1={pk1})'


Error when executing:
Reverse for 'by_par' with arguments '('',)' not found. 1 pattern(s) tried: ['zoom/pr/(?P[0-9]+)/$'

] it converts the digit from pk1 into a string. Correctly?
How can I convert it to int when passing.
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2021-01-09
@SergeiKl

Judging by the text of the error, it does not occur at all when fetching from the database, but when searching for a route.

D
Dr. Bacon, 2021-01-09
@bacon

Why did you make them lines? As the text described before "I did this:", so do if

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question