0

I have the following query:

list = catalog['model'].objects.all().values(*catalog['id_label']).order_by('name')

and I have the following variable: filter='state=1'

How can I change all() for filter(), and make django to use the content of variable filter as input for filter()?

HuLu ViCa
  • 5,077
  • 10
  • 43
  • 93

1 Answers1

0

You can convert your string to dictionary and then use that as the input for the filter:

filter_dict = dict([filter.split('=')])


list = catalog['model'].objects.filter(**filter_dict).values(*catalog['id_label']).order_by('name')

or if you have more than one key=value pair in your filter variable, you can use regex to convert them to dict, as in this answer.

Pedram Parsian
  • 3,750
  • 3
  • 19
  • 34