pythonpython-3.xvariablesredmineredmine-api

Python creating a variable dynamically


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


Solution

  • 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})