Skip to Content
Menu
This question has been flagged
1 Reply
1973 Views

Lets say I have the models setup below where ModelA has multiple ModelB's, and ModelB has one ModelC. I have the ondelete set to cascade, so that if I delete ModelA, it should delete all the references to ModelB, and should delete the references to ModelC on the ModelB's.

What I'm having issues with is creating a ModelA by giving it that data for all the ModelB's and their references to ModelC so that everything is created at once. If I remove the ModelC from this example it will work in creating the ModelsB's and the ModelA. I cannot however seems to create ModelC at the same time.

Is it possible to do a single create for ModelA, and have it create all the ModelB's as well as their reference to ModelC? Below is what the data would look like when creating ModelA, but it gives an error can't adapt type 'dict'. I assume the dict it is referring to is the data for ModelC. 


{name: "test A", b_models: [(0, 0, {name: "test B", c_model: (0,0, {name: "test C"})})]}



-----------------------------------------------------------------

class ModelA(models.Model):

    _name = 'model_a'   

    name = fields.char() 

    b_models = fields.One2many('model_b')


class ModelB(models.Model):

    _name = 'model_b'   

    name = fields.char() 

    c_model = fields.Many2one('model_c', ondelete='cascade')


class ModelC(models.Model):

    _name = 'model_c' 

    name = fields.char() 

    b_model = fields.Many2one('model_b', ondelete='cascade')

-------------------------------------------------------------------------------------

Avatar
Discard
Author Best Answer

I was able to get it to work by following this: https://odoo-development.readthedocs.io/en/latest/dev/py/one2one.html

The field I'm creating on B and C is a OneToOne relationship. Not sure if this is the preferred method going forward, but it works for my scenario.

Avatar
Discard