使用 ASANA Python API 更新自定义字段 [英] Updating a custom field using ASANA Python API

查看:25
本文介绍了使用 ASANA Python API 更新自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新 Asana 列表中自定义字段的值.我正在使用 Asana API v1 的官方 Python 客户端库.

I'm trying to update the values of custom fields in my Asana list. I'm using the Official Python client library for the Asana API v1.

我的代码目前是这样的;

My code currently looks like this;

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

但是当我运行代码时,任务没有更新.打印client.tasks.update的返回返回了任务的所有细节,但是自定义字段没有更新.

But when I run the code, the task doesn't update. The return of print client.tasks.update returns all the details of the task, but the custom field has not been updated.

推荐答案

我认为问题在于我们的 API 在自定义字段方面并不对称……我觉得这有点令人讨厌;在这种情况下,这可能是一个真正的问题.不是像上面那样能够在值块中设置自定义字段的值,这是直观的,您必须使用 custom_field_id:new_value<的 key:value 字典式设置来设置它们/code> - 不幸的是,不是那么直观.所以上面,你有

I think the problem is that our API is not symmetrical with respect to custom fields... which I kind of find to be a bummer; it can be a real gotcha in cases like this. Rather than being able to set the value of a custom field within the block of values as you're doing above, which is intuitive, you have to set them with a key:value dictionary-like setup of custom_field_id:new_value - not as intuitive, unfortunately. So above, where you have

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

我认为你必须做这样的事情:

I think you'd have to do something like this:

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

目标是为看起来像这样的 POST 请求生成 JSON

The goal is to generate JSON for the POST request that looks something like

{
  "data": {
    "custom_fields":{
      "12345678":"New Data!"
    }
  }
}

进一步说明,如果您有文本自定义字段,则该值应该是新的文本字符串,如果是数字自定义字段,则该值应该是新的文本字符串,以及 enum_options 选项的 ID(取查看我们文档站点上此标题下的第三个示例),如果这是一个枚举自定义字段.

As a further note, the value should be the new text string if you have a text custom field, a number if it's a number custom field, and the ID of the enum_options choice (take a look at the third example under this header on our documentation site) if it's an enum custom field.

这篇关于使用 ASANA Python API 更新自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆