Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
3285 Lượt xem

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.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả

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.

Tác giả

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

Tác giả

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'

Tác giả Câu trả lời hay nhất
It's work
Ảnh đại diện
Huỷ bỏ

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

Tác giả

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

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 4 23
2768
2
thg 2 25
40444
2
thg 11 22
2843
1
thg 2 22
7862
2
thg 12 21
4127