Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
1 Відповісти
1737 Переглядів

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

Аватар
Відмінити
Найкраща відповідь

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

Аватар
Відмінити
Related Posts Відповіді Переглядів Дія
1
квіт. 25
1906
1
бер. 25
1716
4
серп. 24
3105
1
січ. 24
2601
1
січ. 24
998