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

Hello,

I want to display value to  another model field with create function.

Any help please?

class MyClass1(models.Model):
    _inherit = 'x.x'
    field1 = fields.Char(string='A')
   
    @api.model
    def create(self, vals):
       #field1
        result = super(MyClass1, self).create(vals)
        return result


class MyClass2(models.Model):
    _inherit = 'z.z'
    field2 = fields.Char(string='B')
   

    @api.model
    def create(self, vals):
        # field2 = field1
        result = super(MyClass2, self).create(vals)
        return result

Thanks.
Avatar
Discard
Best Answer

Based on what condition you want to search the record from "x.x" object? There should be some condition to fetch the single record out of hundreds or thousands of records.

class MyClass2(models.Model):
    _inherit = 'z.z'

@api.model
def create(self, vals):
field1 = self.env['x.x'].search([('field1', '=', 'Some Value')], limit=1).field1
vals.update({'field2': field1})
return super(MyClass2, self).create(vals)

In the example, [('field1', '=', 'Some Value')] is a condition (domain) to search the record from another object.


Avatar
Discard
Related Posts Replies Views Activity
2
Aug 25
2753
3
Feb 25
3964
0
May 24
46
1
Apr 24
3597
4
Sep 23
5150