2016-12-21 46 views
0

私のAsanaリストのカスタムフィールドの値を更新しようとしています。私はOfficial Python client library for the Asana API v1を使用しています。ASANA Python APIを使用したカスタムフィールドの更新

現在、自分のコードは次のようになっています。

project = "Example Project" 
keyword = "Example Task" 

print "Logging into ASANA" 
api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 
client = asana.Client.basic_auth(api_key) 
me = client.users.me() 
all_projects = next(workspace for workspace in me['workspaces']) 
projects = client.projects.find_by_workspace(all_projects['id']) 

for project in projects: 
    if 'Example Project' not in project['name']: 
     continue 
    print "Project found." 
    print "\t"+project['name'] 
    print 

    tasks = client.tasks.find_by_project(project['id'], {"opt_fields":"this.name,custom_fields"}, iterator_type=None) 

    for task in tasks: 
     if keyword in task['name']: 
      print "Task found:" 
      print "\t"+str(task) 
      print 
      for custom_field in task['custom_fields']: 
       custom_field['text_value'] = "New Data!" 
      print client.tasks.update(task['id'], {'data':task}) 

しかし、コードを実行すると、タスクは更新されません。 print client.tasks.updateの戻り値はタスクのすべての詳細を返しますが、カスタムフィールドは更新されていません。

答えて

1

私の問題は、私たちのAPIがカスタムフィールドに関して対称ではないと思います。このような場合には、本当の邪魔になることがあります。上記のように直感的な値のブロック内にカスタムフィールドの値を設定できるのではなく、キー:値辞書のような設定custom_field_id:new_valueで設定する必要があります - 残念ながら直感的ではありません。したがって、上記の、あなたは

for custom_field in task['custom_fields']: 
    custom_field['text_value'] = "New Data!" 

を持っているところ、私はあなたがこのような何かをしなければならないと思う:

new_custom_fields = {} 
for custom_field in task['custom_fields']: 
    new_custom_fields[custom_field['id']] = "New Data!" 
task['custom_fields'] = new_custom_fields 

目標は

{ 
    "data": { 
    "custom_fields":{ 
     "12345678":"New Data!" 
    } 
    } 
} 
のようになりますPOSTリクエストのためのJSONを生成することです

さらに、テキストのカスタムフィールドがある場合は新しいテキスト文字列、数字のカスタムフィールドの場合は数字、enum_optionsのIDの場合は値が必要です(thir dの例は、ドキュメントサイトのthis headerの下にあります)。

+0

私にとってはうまくいきません。 – KSmith

+0

client.tasks.update(task ['id']、{'data':task})の結果は、元のタスクを返すように見えます。 – KSmith

0

マットのおかげで、私は解決に達しました。

new_custom_fields = {} 
for custom_field in task['custom_fields']: 
    new_custom_fields[custom_field['id']] = "New Data!" 

print client.tasks.update(task['id'], {'custom_fields':new_custom_fields}) 

オリジナルのコードには2つの問題がありました。まず、APIを対称的に扱おうとしていましたが、これはMattによって特定され解決されました。もう1つは、間違った形式で更新しようとしていたことです。元のコードと更新されたコードのclient.tasks.updateの違いに注意してください。

関連する問題