3

I wrote a class that check 4 parameters in input and shows a list a result in output. Only one of this parameter is required, therefore i have 7 if - else nested blocks. I want to specify that the condition as stated works properly.

I was wandering if there was a smarter way to write this:

if cd['subject'] is None:
    if cd['school'] == '':
        if cd['price']:
            files = File.objects.filter(name__contains=cd['name'], price = '0.0')
        else:
            files = File.objects.filter(name__contains=cd['name'])
    else:
        if cd['price']:
            files = File.objects.filter(name__contains=cd['name'], school = cd['school'], price = '0.0')
        else:
            files = File.objects.filter(name__contains=cd['name'], school = cd['school'])
else:
    if cd['school'] == '':
        if cd['price']:
            files = File.objects.filter(name__contains=cd['name'], subject = cd['subject'], price = '0.0')
        else:
            files = File.objects.filter(name__contains=cd['name'], subject = cd['subject'])
    else:
        if cd['price']:
            files = File.objects.filter(name__contains=cd['name'], school = cd['school'], subject = cd['subject'], price = '0.0')
        else:
            files = File.objects.filter(name__contains=cd['name'], school = cd['school'], subject = cd['subject'])
return render(request, 'search.html', {'files': files, 'request': request})
4
  • Please fix your code indentation; why are you mixing None, empty string '' and boolean coersion (if <variable>) in comparaisons? Commented Dec 23, 2013 at 21:03
  • Because the variables come from a Django form and are different form fields. I forgot to say, the condition works just fine, I was wondering if there was a smarter (or more elegant if you may) way to say the same thing. Commented Dec 23, 2013 at 21:06
  • Very strange cd['price'] handling Commented Dec 23, 2013 at 21:10
  • 1
    This might be better suited on codereview.stackexchange.com. Commented Dec 23, 2013 at 21:10

2 Answers 2

6

Internally, the keyword arguments you're passing to the function are just a dict. So build it yourself and pass it to the function using the **name syntax:

args = {}

args['name__contains'] = cd['name']

if cd['subject'] is not None:
    args['subject'] = cd['subject']
if cd['school'] != '':
    args['school'] = cd['school']
if cd['price']:
    args['price'] = cd['price']

files = File.objects.filter(**args)
return render(request, 'search.html', {'files': files, 'request': request})
Sign up to request clarification or add additional context in comments.

1 Comment

Tuned a little the condition and worked like a charm.
0

Build a dict with the keyword args for your call to filter(), then pass it using the **kwargs syntax.

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.