0

使用 python 请求,我请求了一个 API 表单 Zoho:

tags = requests.get(' https://books.zoho.com/api/v3/setting/tags?organization_id=xx&authtoken=xxx ').json()

返回此数据:

 "reporting_tag": {
    "tag_id": "1717893000000000335",
    "tag_name": "division",
    "associated_with": "item",
    "is_active": true,
    "status": "active",
    "tag_options": [
        {
            "tag_option_id": "1717893000000123005",
            "tag_option_name": "A",
            "is_active": true,
            "status": "active"
        },
        {
            "tag_option_id": "1717893000000123003",
            "tag_option_name": "B",
            "is_active": true,
            "status": "active"
        },
        {
            "tag_option_id": "1717893000000123007",
            "tag_option_name": "C",
            "is_active": true,
            "status": "active"
        }
    ]  

从上面的结果,我需要存储(tag_name)和它的(tag_option_name)。API 可能会产生数百个 tag_name 及其 tag_name_options。

到目前为止,我已经定义了 Many2one 字段来仅存储 tag_name:

x_tag_name = fields.Many2one('zoho.tags', string="标签名称")

所以我想做的是当我选择一个 tag_name 时,它​​的所有 tag_option_name 都应该出现在另一个 Many2one 字段中。类似于一个父母有多个孩子。我不知道这是否可能,我希望你能帮助我做类似的场景。

ZohoTags 类(模型。模型):

  _name = 'zoho.tags'

  name = fields.Char(string="Tags") 
  tag_options = fields.Char(string='Options')
  tag_id = fields.Char(string="Tag Id")

  @api.multi
  def tags_get(self):
      token = ''  
      org_id = ''

      setting_values = self.env['ir.config_parameter'].search([])
      for keys in setting_values:
        if keys.key == 'account.zoho_authtoken_module':
           token = keys.value
           print(keys.value)
        if keys.key == 'account.zoho_organization_module':
           org_id = keys.value
           print(keys.value)


      tags = requests.get('https://books.zoho.com/api/v3/settings/tags?organization_id=xxx&authtoken=xxx').json()
      for data in tags['reporting_tags']:
        tag_name = '%s' % (data['tag_name'])
        tag_ids = '%s' % (data['tag_id'])
        self.env.cr.execute("INSERT INTO zoho_tags (name, tag_id) VALUES ('%s', '%s')" % (tag_name, tag_ids))
        self.env.cr.commit()
        print(tag_name)

类标签线(模型。模型):

  _name = 'zoho.tags.line'

  x_tag_name = fields.Many2one('zoho.tags', string='Analytic Account')
  x_tags_options = fields.Char(string='Tags Option', related="x_zoho_tags.tag_options")
  rules_id = fields.Many2one('hr.salary.rule')
4

1 回答 1

0

是的,你的目标肯定是可以实现的。

所以我想做的是当我选择一个 tag_name 时,它​​的所有 tag_option_name 都应该出现在另一个 Many2one

您的tag_option_name,让我们假设该数据的模型是,将与zoho.tags.lineMany2one关系,而不是相反。将与有相反的关系,并设置为。因此,对于每条记录,您将从字段中获得多条记录,这些记录可以显示在表单视图中。tag_idzoho.tagszoho.tagsOne2manytag_option_idszoho.tags.lineinverse_keytag_idzoho.tagszoho.tags.linetag_option_idstree/list

  _name = 'zoho.tags'

  name = fields.Char(string="Tags") 
  tag_option_ids = fields.One2many(zoho.tags.line, tag_id, string='Options')
  tag_id = fields.Char(string="Tag Id")



  _name = 'zoho.tags.line'

  tag_id = fields.Many2one('zoho.tags', string='Analytic Account')
  x_tags_options = fields.Char(string='Tags Option', related="x_zoho_tags.tag_options")
  rules_id = fields.Many2one('hr.salary.rule')
于 2019-03-14T10:26:38.840 回答