How can we create One2many field in a Transient Model ?

Hello Community, 

I'm facing a similar problem like this question 


So anyone suggests how to achieve this one?

2 Respostas
Best Answer

Hello all, 

My Issue fix after apply this one 

First, can you share why you want to use the field of TransientModel and if you want to use the field you need to change the class from class sales_test_wizard(models.TransientModel) to class sales_test_wizard(models.Model).


Aktiv Software

1 Comentário
Oleksandr Komarov

Thus, your "temporary"(transient) model will be constantly stored in the database.

When in the "transient" model Form you press ANY(!) button (except the Cancel button) - odoo your record is saved.

Thus, records accumulate every call of your "Master".

You need:

or do a “planned job” that will clean this garbage,

or make a special button (using JavaScript), which, when clicked, will not save the model to the database, but will run function what you needed.

Oleksandr Komarov
Best Answer

I agree with you. But I have another two ways:


In Transient model change field type o2m to m2m. Example:

class MyWizard(models.TransientModel):
_name = 'my.best.wizard'
_description = 'Just Best Wizard'

field_ids = fields.Many2many('res.partner', 'my_best_wizard_rel', 'partner_id', 'my_best_wizard_id', string='Partners')

It's the best and simplest solution.

The second is to:

  1. create additional(2nd) Transient model (named like: 'first_transient_model_name.line')

  2. make new field o2m from 1st Transient model to 2nd

  3. In 2nd transient model add your field to your real model with type m2o

  4. ...

  5. PROFIT(?)

Look example in: /mail/wizard/mail_resend_wizard.py

But using this method, you lose the ability to directly select the lines of your real Model in the form of a transient model.
You also lose the ability to select several lines of the your real Model from the list at once.

Although, you can make a "button" in transient model Form, which will run modal list-form of your real Model, for allowing selecting several rows you need at once...

It's not an easy way, man.