Skip to Content
Menu
This question has been flagged
2 Replies
4089 Views

Hi,


I need to send a value from one form to another form via field to set a default value in a record that I am creating using a many2one field.


Imagine I have 3 models parking_space, driver and car. A driver owns cars and also owns parking spaces. A parking space has one owner and one car (for the sake of this example). 


Now I am in the form of the parking_space and want to create a car using the field of this form. After clicking Create or Edit (in the car field) the car form pops and I want to have the owner field of this car set as the owner of the parking_space. So I guess I need to send the owner of the parking space via the car field using the context attribute but I can't get it working.


I have tried something like this:


<field name="car" context="{'default_owner_name':owner_name}" />


But the owner field in the car form is empty.


I also have tried sending a value via context and having the owner field in the car model with a default attribute that calls a method that checks the context to set the value of the field, something like this:


<field name="car" context="{'owner_id':owner_name.id}" />


owner_name= fields.Many2one('res.partner', 'Driver',required=True, default=_get_default_owner)


@api.model

    def _get_default_owner(self):

        owner = None

        if self.env.context.get('default_owner_id') != False:

            owner_id= self.env.context.get('owner_id')

            owner = self.env['res.partner'].search([('id', '=', owner_id)])

        return owner


But the context has not my custom value. Can someone help me?


Thank you!

Avatar
Discard
Best Answer

Hi,

This will be the simple solution for the many2one field:

owner_name= fields.Many2one('res.partner', 'Driver',required=True, default=lambda self: self.env.partner_id)

Regards,


Avatar
Discard
Best Answer

Use  context in xml make sure to add "default_" before the field name


Avatar
Discard