Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
2405 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
1
dub 25
2541
1
bře 25
2298
4
srp 24
3742
1
led 24
3399
1
led 24
1596