Help

0

return field value from one2many field to parent model

Avatar
moh

I want to fetch field value from one2many field and return it to field in parent model:

class test_line(models.Model)

_name='test.line'

name = fields.Char('name')

test_id = fields.Char('Test')

t = fields.Char('T')

class test_parent(models.Model):

test_line_id = fields.One2many('test.line','test_id')

name = fields.Char('name')


** How to return field name in Class test.line to field name in Class test_parent

How i can to solve this Please Help 


Thanks        

1 Comment
Avatar
Discard
Avatar
Samo Arko
-

first learn how to correctly do an inverse relation. You also are missing the _name for your parrent model test_id = fields.Many2one('_name value').

Go and learn the basics about relations and models.

def get_name(self):

return self.test_id.name

If you want as a field you can use a relation field. Go read the official documentation and start doing the examples.

1 Answer
0
Avatar
Justin Chen
Best Answer

Hi, I am not allowed to comment under your post.

Every One2many field requires a Many2one field.

Model 'parent' has a One2many, then Model 'child' must have a Many2one responding to it.

class TestParent(models.Model):
    _name = 'test.parent'
    name = fields.Char('Name') 
    child_id = fields.One2many('test.child', 'parent_id')

class TestChild(models.Model):
    _name = 'test.child'
    name = fields.Char('Name')
    parent_id = fields.Many2one('test.parent', 'Parent ID')

BOLD text is the key that properly relates two models.

For instance, in parent, you can have tree views, then the child is what's available as columns in that tree view.

Avatar
Discard