This question has been flagged

i'am added a car field in product template which related  to the car model that contains all cars, also i'am work in multi compny, and i'am also added a many2many relation with car model, 
all i need is to filter the cars in product template and display all cars that i have been assign in the company  

this my code 


class cars(models.Model):
_name = 'cars'

name = fields.Char( string="Car",translate=True , required=True, ondelete='restrict')
class autopartscompany(models.Model):
_inherit = 'res.company'

car_ids = fields.Many2many(comodel_name="cars",string="Cars" )
class autopart(models.Model):
_inherit = 'product.template'
car = fields.Many2one(comodel_name="cars", store=True, string="Car", ondelete='restrict', required=False, domain="[('name','=', self.company_id.car_ids )]")
can any one help me how to define the domain filter correctly because that one give me error  Error: NameError: name 'self' is not defined
Avatar
Discard
Best Answer

Hi Mohamed,

Go with the below defined domain code

def _get_cars_domain(self):
return [('id', 'in', self.company_id.car_ids.ids)]

car = fields.Many2one(comodel_name="cars", store=True, string="Car", ondelete='restrict', required=False, domain=_get_cars_domain)

Thanks.

Avatar
Discard
Author

thanks dear for your help but it dosn't return any result

Check this -->

def _get_cars_domain(self):

return [('id', 'in', self.env.user.company_id.car_ids.ids)]

Thanks