Skip to Content
Menu
This question has been flagged
2 Replies
2095 Views

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.

Avatar
Discard
Best Answer

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

Avatar
Discard
Author

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.

Author

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

Author

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'

Author Best Answer
It's work
Avatar
Discard

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

Author

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

Related Posts Replies Views Activity
2
Apr 23
1452
2
May 24
35813
2
Nov 22
1479
1
Feb 22
6687
2
Dec 21
2706