Django: How to get parameters used in filter() from a QuerySet?

For the following QuerySet,

qs = MyModel.objects.filter(attribute_one='value 1', attribute_two='value 2')

How can I retrieve the parameters used in the filter (i.e. attribute_one and attribute_two) and the corresponding values (i.e. value 1 and value 2) from qs?

Thank you!


Solution 1:

You can use values as mentioned in comment. From the docs:

Returns a QuerySet that returns dictionaries, rather than model instances, when used as an iterable.

qs = MyModel.objects.filter(
   attribute_one='value 1', attribute_two='value 2'
).values('attribute_one', 'attribute_two')

for q in qs:
  for key, value in q.items():
    # key as attribute_one
    # value as value_1
    # and so on