Skip to Content
Menu
This question has been flagged
4 Replies
10262 Views

Hello,


I am trying to show in the edit view of a model, a list of records that belong to a field that is also a list of records.


I will try to give an example of what I am trying to achieve to clarify my problem.


Lets say I have a model "Travel", one travel has a list of cars and every car has a list of drivers. I need to show the list of drivers of a travel in its edit view. The model "Travel" has a  One2Many field called cars for its cars, and the model "Car" has also a One2Many field called driver for its drivers. 


If I try to use the field cars.drivers in the view I get an error because the model "Travel" has no field called cars.drivers.


If I create in my model "Travel" a field called, for example, travel_drivers as a One2Many field of drivers that is related to cars.drivers, the list shows the drivers of the first car in my field cars, but it does not show all the drivers from all the cars.


How can I achieve this?


Thank you.



Avatar
Discard
Best Answer

This seems bit complex but the easy way is to add a Smart button in your form view of Travel and when user clicks on it, return an action of the Driver object to show all the drivers of the cars in a tree view.

@api.multi
def view_drivers(self)
return {
'name': 'Drivers',
'view_type': 'form',
'view_mode': 'tree',
'res_model': 'car.driver',
'type': 'ir.actions.act_window',
'domain': [('id', 'in', driver_ids)], # List of IDs of the Drivers
}


One2many Compute:

driver_ids = fields.One2many('driver.driver', compute='_compute_driver_ids')

def _compute_driver_ids(self):
    # You need to search the IDs of the drivers
    self.driver_ids = recordset_of_drivers


Avatar
Discard
Author

So if I want to use "pages" to divide the information in tabs like, for example, one tab of "General information", another tab of "Cars" and one last tab of "Drivers", the first two tabs will work but for "Drivers" the user has to click a button in order to see the list, right?

Well, if there is no other way I will try it, thank you!

There is another way to have those records in a O2m field in your Travel form.

You can create O2m field with compute. See my updated answer.

Author

Ok, I have done it using a One2Many field with compute. I will add an answer with an example and a link and mark it as a solution.

Thank you!

Author

I didn't see your updated answer. I mark it as best answer.

This link has also info about One2Many field with compute: https://www.odoo.com/es_ES/forum/ayuda-1/question/one2many-related-field-165974

Thank you.