1
votes

When click on Add an Item in tree view, I want in new row copy value from last inserted row.

Eg. if field name = 'Text' in new row I need in field name string 'Text'

Any simple solution?

1

1 Answers

2
votes

If you want to load default value from a database then follow this method.

You can achieve it by overriding default_get method and in that, you need to write your logic.

@api.model
def default_get(self,fields):
    res = super(class_name, self).default_get(fields)
    last_rec = self.search([], order='id desc', limit=1)
    if last_rec:
        res.update({'your_field_name':last_rec.field_value})
    return res

While you click on add an item it will fill the new record with its default value and in default value we have written last record's value it it's there.

If you want to load default value from list view (last added value in a list) then it's a bit tricky work, for that you can do something like as follow.

Add one field in the parent form.

last_added_value = fields.Char("Last Added Value")

Create onchange method for that field.

@api.onchange('field_name')
def onchange_fieldname(self):
    # there must be many2one field of parent model, use it here.
    self.parent_model_field.last_added_value = self.field_name

And in xml field, you need to write like this.

<field name="one2many_field" context="{'default_field_name' : parent.last_added_value}">
    <tree string="Title" editable="bottom">
        <field name="field_name"/>
    </tree>
</field>

You also need to write default_get method.

@api.model
def default_get(self,fields):
    res = super(class_name, self).default_get(fields)
    last_rec = self.search([('parent_field_id','=',self.parent_field_id.id)], order='id desc', limit=1)
    if last_rec:
        res.update({'your_field_name':last_rec.field_value})
    return res