2

Question 1. how could I use a variable in this queryset,

def function(request):
    qset1 = BigTable.objects.values(
        'deb_nr_f__dcountry__wcode_f__code',
        'deb_nr_f__dcountry__wcode_f__code_name',
        ).filter(id_nr_f__ie_code__unomer_f__nomer=iteration_result,
        )

I mean something like that

def function(request):

    x1 = 'id_nr_f__ie_code__unomer_f__nomer'

    qset1 = BigTable.objects.values(
        'deb_nr_f__dcountry__wcode_f__code',
        'deb_nr_f__dcountry__wcode_f__code_name',
        ).filter(x1 = iteration_result,
        )

Question 2. How can I empty the filter with a varianbe, so the the filter in the query does not filter the records. I mean something like that

def function(request):

    x1 = 'id_nr_f__ie_code__unomer_f__nomer'
    x2 = True

    qset1 = BigTable.objects.values(
        'deb_nr_f__dcountry__wcode_f__code',
        'deb_nr_f__dcountry__wcode_f__code_name',
        ).filter(x1 = x2,
        )

Thank you.

2 Answers 2

2

you can define a dict, then pass it to filter as kwargs

attr1 = 'some attr'
attr2 = 'some attr'
filter_dict = {
    attr1:filter1,
    attr2:filter2,
     ...
}
queryset.filter(**filter_dict)
Sign up to request clarification or add additional context in comments.

Comments

1

You can make use of a Q object where you pass a 2-tuple you aim to filter:

from django.db.models import Q

def function(request):

    x1 = 'id_nr_f__ie_code__unomer_f__nomer'

    qset1 = BigTable.objects.values(
        'deb_nr_f__dcountry__wcode_f__code',
        'deb_nr_f__dcountry__wcode_f__code_name',
    ).filter(
        Q((x1, iteration_result))
    )

or with a value:

from django.db.models import Q

def function(request):

    x1 = 'id_nr_f__ie_code__unomer_f__nomer'
    x2 = True

    qset1 = BigTable.objects.values(
        'deb_nr_f__dcountry__wcode_f__code',
        'deb_nr_f__dcountry__wcode_f__code_name',
    ).filter(
        Q((x1, x2))
    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.