This question has been flagged

I am trying to set a conditional default for the field 'Fiscal Position' (.property_account_position_id) in res.partner model based on the Country (country_id) selected for that contact. If Country is "United States (233)", Fiscal Position is "Fiscal Position for United States (3)"; else Fiscal Position is "International (5)"

Can someone tell me what I am doing wrong?

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

class ResPartner(models.Model):
    _name = ‘res.partner’
    _inherit = ‘res.partner’

    country_id = fields.Many2one(‘res.country’, string=‘Country’,
                                help=“Apply only if delivery or invoicing country match.“)
    property_account_position_id = fields.Many2one(‘account.fiscal.position’, company_dependent=True,
                                                    string=“Fiscal Position”,help=“The fiscal position will determine taxes and accounts used for the partner.“,oldname=“property_account_position”,readonly=False)
    @api.onchange(‘country_id’)
    def assign_fiscal_position(self):
        for record in self:if record.country_id == 233:
                record.property_account_position_id = 3
        else:
                record.property_account_position_id = 5

Avatar
Discard
Best Answer

I have several observations:

(1) country_id is already defined for res.partner.  Do you mean to change the definition?  This is the existing definition:

country_id = fields.Many2one('res.country', string='Country', ondelete='restrict')

Odoo will already use this field, so if you want your own you will need a different name.

(2) country_id is a Many2one to res.country - you compare a res.country instance with an integer.  You should be comparing the id of that instance with an integer, like:

if record.country_id.id == 233:

(3) property_account_position_id is also a Many2one - to account.fiscal.position - you compare it with an integer. Follow the same advice for the res.country comparison.

(4) 233 is hardcoding the country_id - it is best practice to search for the country.


I think some training and/or review of existing code might help you get up to speed more quickly.

 


Avatar
Discard