3
votes

I have one form with fields,

Name :
First Name:
Second Name:
Last Name:

I have written create function, when we enter first name, second name, last name, it will set the name

First Name: Test First
Second Name: Test Second
Last Name: Test Third

it will generate

Name : Test First Test Second Test Third

This is because of create function.

@api.model
def create(self, vals):
    child = vals.get('child_first_name')
    child_middle = vals.get('child_middle_name') or ' '
    child_last = vals.get('child_last_name') or ' '
    if child:
        vals['name'] = self.env['ir.sequence'].next_by_code('application_form')
        vals['child_name'] = child + ' ' + child_middle + ' ' + child_last
    return super(application_form, self).create(vals)

When I edit any of these fields

First Name
Second Name
Last Name

It should set the name again.

Kindly suggest one write function with good way.

In the same Way:

Question 2 :

There is one boolean field.

For Reference:

I have written sequence Number to be generated in create method. If I uncheck that boolean fields, other set of Sequence number to be generated.

Kindly suggest write function for both.

For Writing the above question

@api.multi
def write(self, vals):
    application = self.browse(self.id)
    if vals.has_key('child_first_name'):
        fname = vals.get('child_first_name') or ' '
    else :
        fname = application.child_first_name
    if vals.has_key('child_middle_name'):
        mname = vals.get('child_middle_name') or ' '
    else :
        mname = application.child_middle_name
    if vals.has_key('child_last_name'):
        lname = vals.get('child_last_name') or ' '
    else :
        lname = application.child_last_name
    full_name = fname + ' ' + mname + ' ' + lname
    vals.update({'child_name':full_name})
    return super(application_form, self).write(vals)
1
Thanks for editingHariharan Srinivasan

1 Answers

1
votes

Here is an example of how you can achieve this

@api.multi
def write(self, vals):
    # checks that new field value
    # is not equal to old one
    def _is_changed(name):
        return name in vals and self[name] != vals[name]

    # returns new field value if present
    # otherwise return old value if present
    # else return default value
    def _get_field(name, default=''):
        return vals.get(name, self[name]) or default

    # choose which sequence to use based on boolean seq_flag
    seq = 'some_seq' if _get_field('seq_flag', False) else 'application_form'

    # if one of name parts was changed
    if _is_changed('child_first_name') or \
       _is_changed('child_middle_name') or \
       _is_changed('child_last_name'):
        vals['name'] = self.env['ir.sequence'].next_by_code(seq)
        name_parts = [
            _get_field('child_first'), 
            _get_field('child_middle_name'), 
            _get_field('child_last_name')
        ]
        # write only non-empty name parts
        vals['child_name'] = " ".join([_ for _ in name_parts if _])
    return super(application_form, self).write(vals)

Also you can use computed child_last_name field and @api.depends to solve first part of your question

child_name = fields.Char(compute='_compute_name')

@api.one
@api.depends('child_first', 'child_middle_name', 'child_last_name')
def _compute_upper(self):
    name_parts = [
        self.child_first, 
        self.child_middle_name, 
        self.child_last_name
    ]
    self.child_name = " ".join([_ for _ in name_parts if _])