Skip to Content
Menu
This question has been flagged
7 Replies
13069 Views

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?


Avatar
Discard
Author Best Answer

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>


Avatar
Discard
Best Answer

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,

Avatar
Discard
Author

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.

Author

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

Best Answer

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,

Avatar
Discard
Author

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

Author

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