Skip to Content
Menu
This question has been flagged
2 Replies
3304 Rodiniai

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.

Portretas
Atmesti
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

Portretas
Atmesti
Autorius

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.

Autorius

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

Autorius

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'

Autorius Best Answer
It's work
Portretas
Atmesti

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

Autorius

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

Related Posts Replies Rodiniai Veikla
2
bal. 23
2789
2
vas. 25
40476
2
lapkr. 22
2861
1
vas. 22
7900
2
gruod. 21
4150