Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odgovori
10236 Prikazi

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
Opusti
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
Opusti
Related Posts Odgovori Prikazi Aktivnost
2
avg. 25
2685
3
feb. 25
3931
0
maj 24
46
1
apr. 24
3576
4
sep. 23
5130