0
votes

I have an inherited model from res.partner.bank which i added a custom field called location to, this is how the model looks like.

class SalesPartnerBankInherit(models.Model):
    _inherit = 'res.partner.bank'

    location = fields.Char()

I have another inherited model from sale.order with a field that has a many2one relationship to res.partner.bank I want to show only 1 record on the many2one dropdown if there are more than 1 records in res.partner.bank that has the same location field value? This is my inherited sale.order model with the many2one location_id field

location_id = fields.Many2one('res.partner.bank',
                               string='Bank Account Location')

So if there are duplicate res.partner.bank records with the same location, I want to show only 1 record on the dropdown.

1
Can you give some examples? - CZoellner
it there are records in res.partner.bank with the same location filled, I want only one record from res.partner.bank to be displayed on dropdown. - A.Sasori
Yes i did understand this, but it's not helping. Please give examples, and please - that's important - try to explain WHY you want one record and not the other. - CZoellner
I really don't know what examples you want me to give. - A.Sasori
how is the location defined on the res.partner.bank - Paxmees

1 Answers

0
votes

Its probably possible if you overwrite name_search() on the 'res.partner.bank' And if you give it specific context then it will only return what you need.

But it would be better if you do it with enumeration.

class Locations(models.Model):
    _name = "res.partner.bank.location"
    _description = "Bank Locations"

    name = fields.Char("Tag Name", required=True)

    _sql_constraints = [
        ("name_uniq", "unique (name)", "Location name already exists!"),
    ]
    
class SalesPartnerBankInherit(models.Model):
    _inherit = 'res.partner.bank'

    location_id = fields.Many2one('res.partner.bank.location', string='Bank Account Location')
location_id = fields.Many2one('res.partner.bank.location', string='Bank Account Location')

#and if you want you can get an example bank object for a location
location_bank = fields.Many2one('res.partner.bank', 
                string='Bank', 
                compute='_compute_bank', 
                store=True, 
                readonly=True)

@api.depends('location_id')
def _compute_bank(self):
    for record in self:
        record.location_bank = self.env["res.partner.bank"].search(
                ["location_id"],"=",record.location_id.id],limit=1) or False