How to chain records of the same Model?

I want to create many chains of the same type records that keep some data in common.
For example:
report id_r:1-> report id_r:1 yesterday <-> report id_r:1 before yesterday
report id_r:2-> report id_r:2 yesterday -> report id_r:2 before yesterday
They are all the same Model.
'my_module.report'
How to declare it??

class Report():
    __name__ = 'mymodule.report'

next_report = fields.One2One( ???
previous_report = fields.One2One( ???
##<fields>

OR

next_report = fields.Many2Many( ???
previous_report = fields.Many2Many( ???
##<fields>

class ChainReports():
next_report =  ????
prev_report =  ????

I don’t clearly understand how to correctly build such a relation???
Maybe by using a Function field???

Constantine.

You can use the UnionMixin to create a new model which will contain the record of both models. With this setup you can then for the records of the UnionModel in your report.

You have an example of how to setup an union model on the server test suite.

Hope it helps.

You can use the UnionMixin

That is an interesting idea…
Will try that.
———
Why doesn’t work the simple approach of just creating a field that points to another id of the same table???

It is difficult to answer because we are missing the context. Instead of asking how to implement what you think is the solution, you should explain what you are trying to solve.

I have a rental fleet and inspections of these cars. I want to have in each inspection record a link to the previous inspection, something like that:

...
{id 22} | {inspection at <datetime>}  | {car id} | {inspection result} | {previous inspection <id>=17}
{id 23} | {inspection at <datetime>}  | {car id} | {inspection result} | {previous inspection <id>=4}
{id 24} | {inspection at <datetime>}  | {car id} | {inspection result} | {previous inspection <id>=23}
{id 25} | {inspection at <datetime>}  | {car id} | {inspection result} | {previous inspection <id>=22}
...
{id 1010} | {inspection at <datetime>}  | {car id} | {inspection result} | {previous inspection <id>=20}
{id 1011} | {inspection at <datetime>}  | {car id} | {inspection result} | {previous inspection <id>=25}
{id 1012} | {inspection at <datetime>}  | {car id} | {inspection result} | {previous inspection <id>=100}

image
Note: there are many link chains in the same table (in parallel — as seen above).

————————————
Now, I solved that in the other way: I created a separate model with inspection results and link (O2M<->M2O) results with inspection occasions accordingly:

Constantine.

I do not see the point to have a link as long as you can order the records per date.
As far as I understand you just need to display for a car the One2Many of inspection ordered per date.

Not really,
as there are many cars and each has its inspections in an arbitrary moment, in result inspections sorted by date-time will result in a mix of unrelated inspections. I redraw the picture in a better way:

I said that it is a One2Many on the car. So you order the list of only one car.

1 Like

In that case:

Hmmm…
I need to combine all inspections in one record ‘Vehicle’.
Ah! The problem was that Inspection results are not ordered in the correct way (overlaping), in the result — I will lose the general order of inspections, that was kept in the Inspections model.
Also, I understand that I skipped a very important point, sorry: each Inspection is a contract that consists of two inspections: Check-out and Check-in. (i.e. This/Previous).
while vehicles were the third class that remembers all these inspections related to a vehicle:

I think your explanation a litte confusion… haha.

I think you can take a look stock_move or account_move.

How customer shipment, supplier shipment, jnternal shipment… sharing one2many records with stock_move.

I agree with @ced, you only need one2 many record by date and with origin(reference)

Best regards

1 Like

I agree with @ced, you only need one2 many record by date and with origin(reference)

Eventually, that is exactly how I did that: I created two One2Many records: This & Previous in the Inspection class. They point to related InspectionReslts, and that is ok for the purpose.