【问题标题】:Podio: How do I use the Item.update-method in Python 3?Podio:如何在 Python 3 中使用 Item.update 方法?
【发布时间】:2017-06-24 11:56:36
【问题描述】:

没有用于 Python 的 Podio API 的示例代码,但这是为 Ruby 编写的示例代码:

Podio::Item.update(210606, {
  :fields => {
    'title' => 'The API documentation is much more funny',
    'business_value' => { :value => 20000, :currency => 'EUR' },
    'due_date' => { :start => '2011-05-06 11:27:20', :end => 
5.days.from_now.to_s(:db) }
  }
})

我一生都想不出如何将它翻译成 Python 3。我尝试过使用字典、列表中的字典、使用字段 ID 和名称等引用字段。但它从来没有实际上更新任何东西。

这是我将上述内容转换为 Python 代码的失败尝试(使用不同的字段,因为我的“错误(API 示例)应用程序”中的字段与示例代码中的字段不同):

newValues = {'fields':{'title': "This is my title",'description_of_problem': 
"the not work"}}

try:
    podio.Item.update(629783395, newValues['fields'])
    print('updating was successful')
except:
    print('updating was not successful')

podio 是:

podio = api.OAuthClient(
    client_id,
    client_secret,
    username,
    password,    
)

我的代码中的'fields' 部分实际上没有任何意义,但我不知道如何处理Ruby 代码的那部分,我怀疑这就是问题所在。该程序总是打印“更新成功”,就好像 Item.update 方法被成功调用一样,但正如我所说,它实际上并没有更新 Podio 中的任何内容。任何人都可以看到有什么问题吗?

【问题讨论】:

  • 'fields' 是最外层字典中的一个键,但您在发送的实际数据中省略了它。

标签: python podio


【解决方案1】:

我只需关注Item update API,并传入与那里的 request 部分匹配的字典:

{
  "revision": The revision of the item that is being updated. This is optional,
  "external_id": The new external_id of the item,
  "fields": The values for each field, see the create item operation for details,
  "file_ids": The list of attachments,
  "tags": The list of tags,
  "reminder": Optional reminder on this task
  {
    "remind_delta": Minutes (integer) to remind before the due date
  },
  "recurrence": The recurrence for the task, if any,
  {
    "name": The name of the recurrence, "weekly", "monthly" or "yearly",
    "config": The configuration for the recurrence, depends on the type
    {
      "days": List of weekdays ("monday", "tuesday", etc) (for "weekly"),
      "repeat_on": When to repeat, "day_of_week" or "day_of_month" (for "monthly")
    },
    "step": The step size, 1 or more,
    "until": The latest date the recurrence should take place
  },
  "linked_account_id": The linked account to use for meetings,
  "ref" The reference of the item
  {
    "type": The type of reference,
    "id": The id of the reference
  }
}

文档进一步指向item creation API for further examples。请注意该对象在最外层映射中如何具有 "fields" 键。

Ruby 文档所做的只是将该映射构建为 Ruby hash(在 Python 中为 dict),其中包含需要更新的条目; :field 是一个不可变字符串(称为 symbol),它在该哈希中定义一个指向嵌套哈希的键。 Python implementation for the update method 只是将该字典转换为 JSON 帖子正文。

Ruby 代码到 Python 的直接翻译是:

from datetime import datetime, timedelta

podio.item.update(210606, {
  'fields': {
      'title': 'The API documentation is much more funny',
          'business_value': {'value': 20000, 'currency': 'EUR'},
      'due_date': {
          'start': '2011-05-06 11:27:20',
          'end': (datetime.now() + timedelta(days=5)).strftime('%Y-%m-%d %H:%M:%S')}
    }
})

在你的情况下你做错了没有在最外面的字典中包含'fields' 键;您打开了最外面的字典,只在'fields' 下发布了嵌套字典。相反,包括那个外部字典:

newValues = {
    'fields': {
        'title': "This is my title",
        'description_of_problem': "the not work"
    }
}

podio.Item.update(629783395, newValues)

【讨论】:

  • 有效!哇,我不敢相信这是一直以来的问题,我以为我之前尝试过,但遇到异常说更新不成功,我认为这是因为删除 ['fields'] (因为它总是打印 'successful ' 当我保留那部分时),但显然它出现异常的原因是因为我没有使用正确的名称指定字段。正如另一位评论员所说,try/except 代码不是很有帮助。无论如何,非常感谢!
  • @Nils:是的,不要玩口袋妖怪,你不想抓住他们所有。见Why is "except: pass" a bad programming practice?
  • 谢谢,我以后会考虑的。编程还是新手。
猜你喜欢
  • 2018-04-07
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2017-01-29
  • 2018-11-19
  • 1970-01-01
  • 2017-06-25
相关资源
最近更新 更多