0

I'm not a python expert and I encountered an issue working with an already existing API (redmine API but it's not really a problem with the API).

The API give me a method which enable me to get data from a database using a filter to get the type of data I need. For example, if i want a data with a value of "value" in the field which have the id = 20

object.issue.filter(cf_20 = value)

It works fine. However the id of the field I need to get can vary. So I have a variable

fieldID

which contains the id of the field I need to filter (20 in my example).

How can I give to object.issue.filter the good value (depending on the variable fieldID) ? Something like

object.issue.filter(cf_<fieldID> = value)

where would be replaced by the value of fieldID

Peni
  • 626
  • 1
  • 7
  • 18
  • take a look at keyword-arguments for functions: https://stackoverflow.com/documentation/python/228/functions/932/defining-a-function-with-an-arbitrary-number-of-arguments i.e. you can dynamically create a dictionary with your custom keys and values and pour it into the function. – Dave J Jun 30 '17 at 14:23

1 Answers1

1

You can create a dictionary {"cf_" + str(fieldID): value} of your intended filter arguments and then use ** to unpack them in the method call:

object.issue.filter(**{"cf_" + str(fieldID): value})
jwodder
  • 54,758
  • 12
  • 108
  • 124