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

Hi,

I inherited res.patner model to add some fields (is_student and others), and I created a new form view with the new fields.

I would like to override a create method to set (True) as a default value for is_student filed only if I opened it from the new form view.

But when I create a new record from contact form, the default value is always True

Example:

class ResStudent(models.Model):
_inherit = 'res.partner'

is_student = fields.Boolean(string='Is Student')

​@api.model
​def create(self,values):
​values['is_student'] = True
​values['studentid'] = self.env['ir.sequence'].next_by_code('res.partner')
​return super(ResStudent, self).create(values)

Thanks

Avatar
Discard
Best Answer

Hi,

Assuming you have already created a new form view for the res.partner model and you want to associate an action with this form view, you can define an action record in XML. Here's an example:

<record id="action_res_partner_new_form_view" model="ir.actions.act_window">
<field name="name">new.form.view</field>
<field name="res_model">res.partner</field>
<field name="view_mode">tree,form</field>
<field name="context">{'from_new_view': True}</field>
    <field name="target">current</field>
</record>

Replace action_res_partner_new_form_view with the actual name of your action.

The context field, where you pass the context information. In this case, it's setting the key 'from_new_form_view' to True. When you open the action associated with this record, it will use this context, and the create method in your Python code can check for this context to conditionally set the default value for the is_student field.

@api.model
    def create(self, values):
        # Check if the context contains the key indicating the source view
if self._context.get('from_new_view'):
            values['is_student'] = True
values['studentid'] = self.env['ir.sequence'].next_by_code('res.partner')
           
        return super(ResStudent, self).create(values)


Hope it helps

Avatar
Discard
Related Posts Replies Views Activity
1
Apr 25
1842
1
Mar 25
1630
4
Aug 24
3027
1
Jan 24
2502
1
Jan 24
910