跳至內容
選單
此問題已被標幟
2 回覆
3255 瀏覽次數

I have in res.config .settings field many2one and in account.analytic .line field many2one or char.

I'd like that after set a value in res.config .setting this value assing to field in account.analytic line.

頭像
捨棄
最佳答案

Hi Ewdy,

If you need to pass the value while creating account.analytic.line you have to pass the value in default_get method of analytic account line model as this:

@api.model
def default_get
(self, fields):
res = super(ProductTemplate, self).default_get(fields)
settings_many2one_field = self.env['ir.config_parameter'].sudo().get_param('settings_many2one_field')
#browse your many2one field id using this settings_many2one_field from your many2one model
    
field_id = self.env['your_model'].search([('id', '=', settings_many2one_field)]).id

    if 'settings_many2one_field' not in fields:
fields.append('settings_many2one_field')
res['many2one_field_analytic_Account'] = field_id.id
return res

Or else you can use compute method for field in analytic account line model

Thank you

頭像
捨棄
作者

I have a model: MyModel whose inherit from 'account.analytic.line' and another model ResConfigSettings whose inherit from: 'res.config.settings'

In MyModel is first_field(many2one), In ResConfigSettings is second_field(Many2one)
And when wrtie:

@api.model
def default_get(self, fields):
res = super(MyModel, self).default_get(fields)
second_field = self.env['ir.config_parameter'].sudo().get_param('second_field')
if 'second' not in fields:
fields.append('second_field')
res['first'] = second_field.id
return res

I get a error:

res['first_field'] = second_field.id
Exception raise exception.with_traceback(None) from new_cause
AttributeError: 'str' object has no attribute 'id'

In addition, I wonder if it can be done using the method, since the fields are in other models.

作者

raise exception.with_traceback(None) from new_cause
AttributeError: 'bool' object has no attribute 'id'

# -*- coding: utf-8 -*-
from odoo import models, fields, api

class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
assign_tax = fields.Boolean('Assign Tax Group', default=False, store=True, readonly=False)
tax_group = fields.Many2one('account.account', 'Tax group', readonly=False, store=True)

(other methods)

class TimesheetAccountAssignment(models.Model):
_description = "Timesheet financial account assignment"
_inherit = "account.analytic.line"

general_account_id = fields.Many2one('account.account', string="Accounting account",
compute='_compute_display_name', readonly=False)

@api.model
def default_get(self, fields):
res = super(TimesheetAccountAssignment, self).default_get(fields)
tax_group = self.env['ir.config_parameter'].sudo().get_param('tax_group')
print(tax_group) # it's int (7)
field_id = self.env['account.analytic.line'].search([('id', '=', tax_group)]).id
print(field_id) # return value bool - False
if 'tax_group' not in fields:
fields.append('tax_group')
res['general_account_id'] = field_id.id
return res

At first check that record exist or not, later access by id
field_id = self.env['account.analytic.line'].search([('id', '=', tax_group)])
if field_id:
//followed by your code

作者

Ok, problem was in field_id = self.env['account.analytic.line'].search([('id', '=', tax_group)]),
should be field_id = self.env['account.account'].search([('id', '=', tax_group)]).

Now I have a:
raise exception.with_traceback(None) from new_cause
ValueError: Invalid field 'tax_group' on model 'account.analytic.line'

作者 最佳答案
It's work
頭像
捨棄

You have to browse the record using the second_field from settings,

作者

I know, only here in the post I didn't add _field

相關帖文 回覆 瀏覽次數 活動
2
4月 23
2721
2
2月 25
40422
2
11月 22
2808
1
2月 22
7840
2
12月 21
4090