Search code examples
odooodoo-15

How to show vehicles as per customers in sale.order form in odoo?


I have inherited res.partner model and views and added one page named Vehicle Details as shown in below image :

res.partner inherited

Simillarly I have inherited sale.order model and views and added one field named vehicles in its form which is shown in below image :

sale.order inherited

Code for inheriting sale.order :

from odoo import api, fields, models


class CustomSaleOrder(models.Model):
_inherit = "sale.order"

x_model_ref_id = fields.Many2one('res.partner.line', string="Model Name")
vehicle_tags = fields.Many2one('res.partner.line', string="Vehicles", domain = [('name','=','partner_id')])

@api.onchange('partner_id')
def onchange_partner_id(self):
    self.vehicle_tags = None
    self.vehicle_tags = self.partner_id.id

The problem I am facing is that I want to fetch vehicles as per customers but it shows all vehicles in all customers. How can I fetch vehicles which specifically belongs to selected cusotmer only ??

I used the domain it shows empty as shown in below image :

After Adding Domain

res.partner.line code :

from odoo import api, fields, models


class CustomContacts(models.Model):
_inherit = "res.partner"

x_brand_ids = fields.Many2many('res.partner.line', 'x_brand_id', string="Brand Name")
x_model_ids = fields.One2many('res.partner.line', 'x_model_id', string="Model Name")
x_customer_ids = fields.One2many('res.partner.line', 'x_customer_id', string="Vehicle Details")

color = fields.Integer(string="Color")  # color_toggle



class CustomContactsPage(models.Model):

_name = "res.partner.line"
_rec_name = 'name'

x_customer_id = fields.Many2one('res.partner', string="Vehicle Details")
x_brand_id = fields.Many2one('vehicle.brand', string="Brand Name")
x_model_id = fields.Many2one('vehicle.model', string="Model Name")    
name = fields.Many2one('vehicle.number',string="Vehicle Number")

Solution

  • Add domain in vehicle_tag field

    vehicle_tags = fields.Many2one('res.partner.line', string="Vehicles",domain="[('partner_id','=',partner_id)")

    partner_id field in left side of domain should be the field that has many2one relation in res.partner.line model you may have named it differently so change it accordingly

    This should only show vehicles of a selected partner in the drop-down as the domain is added to check if the selected partner id and the partner id related to the vehicles are the same

    You might see empty value in the vehicles field if the partner is not selected Added the suggestion in my code

    class CustomContacts(models.Model):
        _inherit = "res.partner"
        x_brand_ids = fields.Many2many('res.partner.line', 'x_brand_id', string="Brand Name")
        x_model_ids = fields.One2many('res.partner.line', 'x_model_id', string="Model Name")
        x_customer_ids = fields.One2many('res.partner.line', 'x_customer_id', string="Vehicle Details")
    
    class CustomContactsPage(models.Model):
        _name = "res.partner.line"
        _rec_name = 'name'
        x_customer_id = fields.Many2one('res.partner', string="Vehicle Details")
        x_brand_id = fields.Many2one('vehicle.brand', string="Brand Name")
        x_model_id = fields.Many2one('vehicle.model', string="Model Name")
        name = fields.Many2one('vehicle.number',string="Vehicle Number")
        
    class VehicleNumber(models.Model):
        _name = "vehicle.number"
        name = fields.Char("Vehicle")
        
    class SaleOrder(models.Model):
        _inherit = 'sale.order'
        vehicle_tags = fields.Many2one('res.partner.line', string="Vehicles",domain="[('x_customer_id','=',partner_id)]")