[英]One record in Many2one field having another multiple records in Many2one field in Odoo
使用python請求,我已經請求了一個API格式Zoho:
標簽= request.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_names及其tag_name_options。
到目前為止,我已經定義了Many2one字段來僅存儲tag_name:
x_tag_name = fields.Many2one('zoho.tags',string =“Tag Name”)
所以我想要做的是當我選擇一個tag_name時,它的tag_option_name應該出現在另一個Many2one字段中。 類似於有多個孩子的父母一方。 我不知道是否有可能,希望您能幫助我做類似的情況。
ZohoTags(models.Model)類:
_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)
class TagsLine(models.Model):
_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')
是的,你的目標肯定是可以實現的。
所以我想要做的是當我選擇一個tag_name時,它的tag_option_name應該出現在另一個Many2one中
你tag_option_name
,假設此數據模型是zoho.tags.line
,將有一個Many2one
關系tag_id
與zoho.tags
,而不是周圍的其他方式。 zoho.tags
將與tag_option_ids
具有反向的One2many
關系zoho.tags.line
,其中inverse_key
設置為tag_id
。 因此,對於每個zoho.tags
記錄,您將從字段tag_option_ids
獲取多個zoho.tags.line
記錄,該記錄可以在表單視圖中的tree/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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.