Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
2440 Widoki

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

Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
kwi 25
2563
1
mar 25
2323
4
sie 24
3763
1
sty 24
3441
1
sty 24
1621