This question has been flagged
2 Replies
3277 Views

I want to automatically fill a table when another table is filled by user.
here's the problem : 

I have different products for rent (let's say cars), each product have different stock/quantity.

I want to develop a method that if a user/admin adds a new "Car Model" with 'stock = N' the method adds N new rows of data for each car.
the reason i need them to be stored separately is because each car has it's own attributes e.g. how many times has it been rented, need repair or not, damaged or not, etc.....

Avatar
Discard
Author Best Answer

Thanks so mush that was helpful
I did the following and it worked

#this code is inside Car Model's class
#Overriding create
@api.model
def create(self, vals):
    res = super(CarModel, self).create(vals)
    for i in range(vals["stock"]):
        super(rc.RentCar, self.env['rent.car']).create({
            'model_id': res.id,
            'car_ref' : "%s_%s" %(res.ref,i+1),
            'name' : "%s #%s" %(res.name,i+1)
        })
    return res

Avatar
Discard
Best Answer

Hi,

You can apply your logic by overriding the create and write method of the first model.

For overriding the create method, see: https://www.youtube.com/watch?v=_-fs_NBeOLI

Thanks

Avatar
Discard