M
M
Maxim Tarabrin2017-10-26 13:26:21
Django
Maxim Tarabrin, 2017-10-26 13:26:21

How to make a filter in django using dictionary and iexact?

Hello, how is it possible to make a filter using a case-insensitive entry and some dictionary from request.GET?
That is, I have a certain form that is built according to the forms.ModelForm model. As a result of receiving, I form a dictionary. and I use it. I give the code below:

filter_map = {}
    def set_if_not_none(mapping, key, value):
        if value is not None and value is not '' and key != 'order_by':
            mapping[key] = value
    for req_key, req_value in dict(request.GET).items():
        set_if_not_none(filter_map,req_key,req_value[0])
    debtors_list = Debtors.objects.filter(**filter_map).order_by(ordering)

This works well only when the fields are specified exactly, but I need to take into account not an exact match, but a case-insensitive occurrence.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rostislav Grigoriev, 2017-10-26
@padr1no

filter_map = {}
def set_if_not_none(mapping, key, value):
    if value is not None and value is not '' and key != 'order_by':
        mapping['{}__iexact'.format(key)] = value
for req_key, req_value in dict(request.GET).items():
    set_if_not_none(filter_map,req_key,req_value[0])
debtors_list = Debtors.objects.filter(**filter_map).order_by(ordering)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question