跳至內容
選單
此問題已被標幟
1 回覆
2418 瀏覽次數

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

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
1
4月 25
2550
1
3月 25
2310
4
8月 24
3747
1
1月 24
3416
1
1月 24
1604