Skip to content
Advertisement

How to apply a for statement to simplify repeated code in Django queryset

The filter is applied according to team_parameter(request.GET.get(‘team)) and it has very repetitive code. At the end of the if statement, no filter is applied only if team_parameter is ‘ALL’. I think a for statement is necessary to minimize this code, but I did not know how to apply it, so I asked a question. Please let me know which loops are needed to simplify the code below. Help.

[views.py]

JavaScript

Advertisement

Answer

You could define the argument lists and keyword argument list before the if-function. And pass these to the database functions:

JavaScript

And than make the queries like this:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement