콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
7 답글
13945 화면

I've extended  the partner model as below.

from openerp import models, fields, api, exceptions
class Partner(models.Model):
_name = 'res.partner'
_inherit = 'res.partner'
@api.model
def _get_partner_category(self):
id = self.env['res.partner.category'].search([['name', '=', 'Registrant']]).id
return [(6,0,[id])]
national_id = fields.Char(string="National ID")
_defaults = {
'category_id': _get_partner_category,
}


As above, the code has made category_id and set the default tag to "Registrant". 


However, this affect other modules that uses the res.partner model.

Can I set defaults on my views instead via the model?


아바타
취소
작성자 베스트 답변

Solution

Create a data xml that has a tag that I want.

        <record model="res.partner.category" id="registrant_tag">
<field name="name">Registrant</field>
</record>


Now on my window action reference the to the tag with an eval option.

 <record model="ir.actions.act_window" id="registrant_list_action">
<field name="name">Registrant</field>
<field name="res_model">res.partner</field>
<field name="domain">[('category_id.name','ilike','Registrant')]</field>
<field name="context" eval="{'default_category_id': [(6,0,[ref('warranty.registrant_tag')])] }"/>
<field name="view_type">form</field>
<field name="view_mode">kanban,tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">Add a registrant
</p>
</field>
</record>


아바타
취소
베스트 답변

Hello,

You can't set default value in View wise.  However you can set default values in Window Action.

For example, 

1. Go to Window Action menu, find "Countries" action and open it.

2. In context field, set "{'default_name':'India'}" 

3. Go to Countries menu Sales >> Configuration >> Address Book >> Localization >> Countries and open it. Try to create new record. You will see name "India" by default.

Tip : In order to set default from front end, You need to add your exact field name followed by "default_" prefix in Window Action's context field.

 Hope this helps,

아바타
취소
작성자

It works partially. In the context field, I set "{'default_category_id':'[(6,0,[18])]'}" where'18' is the "Registrant" index. how do I assigned a function value to it? I tried "{'default_category_id': _get_partner_category}" it doesnt work.

작성자

Thanks for the tip, I have found my solution. :)

베스트 답변

Hello,

Your function _get_partner_category , catch the context that comes from the action, So you can add some custom value to the action's context just like:

{'my_menu': 'my_custom_menu'}

Then in your function check if the context contains the value 'my_menu' if yes then return the calculated value ...


Regards,

아바타
취소
작성자

Sorry, but I still don't quite understand what you mean.? In Emipro's is already using an action and a context. Now you are suggesting to use another action ?

No, no another action. Empiro suggested to use the [ default_ ] in the action's context, to set the defaults value, but you can add a new value to the context. Then add some checks to your function. Just try to print the context in your function to get it . in your function just write: print self._context or self.env.context

작성자

Thanks Ahmed, I think I know where you are getting at but I think I'm quite satisfied with the tip Empiro provide me and manage to achieve what I want