D
D
DeF-F2020-05-12 10:18:08
Django
DeF-F, 2020-05-12 10:18:08

Queryset q object how to write correctly?

f_name = request.GET.get('fname')
    l_name = request.GET.get('lname')
    e_mail = request.GET.get('email')

    if f_name:
        qs = qs.filter(Q(first_name=f_name))
    if l_name:
        qs = qs.filter(Q(last_name=l_name))
    if e_mail:
        qs = qs.filter(Q(email=e_mail))

    result = '<br>'.join(
        str(teacher)
        for teacher in qs
    )

    # return HttpResponse(result)
    return render(
        request=request,
        template_name='teachers_list.html',
        context={'teachers_list': result}
    )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-05-12
@DeF-F

q = Q()   
if f_name:
       q &= Q(first_name=f_name)
if l_name:
       q &= Q(last_name=l_name)
if e_mail:
       q &= Q(email=e_mail)

qs = qs.filter(q)

And read for DjangoFilterBackend
PS And this:
result = '<br>'.join(
        str(teacher)
        for teacher in qs
    )

some kind of game)
do the formatting in the template

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question