如何在 odoo 中以 sale.order 形式根据客户显示车辆?

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

我继承了 res.partner 模型和视图,并添加了一个名为 Vehicle Details 的页面,如下图所示:

同样,我继承了 sale.order 模型和视图,并在其表单中添加了一个名为 vehicles 的字段,如下图所示:

继承代码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

我面临的问题是我想根据客户获取车辆,但它显示了所有客户的所有车辆。我怎样才能获取专门属于选定客户的车辆??

我使用了它显示为空的域,如下图所示:

res.partner.行代码:

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")

在 vehicle_tag 字段中添加域

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

partner_id 域左侧的字段应该是在 res.partner.line 模型中具有多对一关系的字段,您可能对其命名不同,因此相应地更改它

这应该只显示 drop-down 中所选合作伙伴的车辆,因为添加域是为了检查所选合作伙伴 ID 和与车辆相关的合作伙伴 ID 是否相同

如果未选择合作伙伴,您可能会在车辆字段中看到空值

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)]")