تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
4 الردود
22297 أدوات العرض

Hi,

I have a field, say "source_ids" in a form. These items (the sources) have a type, identified with a many2one relation as the type_id field.

In my view, I want to set the default type to an XML_ID (and not the integer ID of the record).

So for instance, my type "FOO" has ID 10, and XML_ID : "ref_module_id.source_type_id".

When I do that, I have an error (it expects an integer) :

<field name="source_id" context="{'default_type_id':'reference_module_id.source_type_id'}"/>

Can anybody help ? I'd really like to avoid setting the numeric value in my view XML...

Thanks

الصورة الرمزية
إهمال
أفضل إجابة

5 years later this is still on top of the results on google search and it still took me all day to find the right solution...

In hope it saves someone else's time:

<field name="source_id" context="{'default_type_id':%(module_name.xml_id)d}"/>

Works on 12...

الصورة الرمزية
إهمال
أفضل إجابة

hi,

can you just try like below ?

<field name="source_id" context="{'default_type_id':ref('reference_module_id.source_type_id')}"/>

I hope it will resolve your issue.

الصورة الرمزية
إهمال
الكاتب

Hi, I did try that before, but it raises this error : Uncaught Error: NameError: name 'ref' is not defined

أفضل إجابة

Hello Henri-Maxime, I'm afraid you can't use `ref()` inside context XML attributes.

You can however define the context in actions and use `ref()` combined with the `eval` XML attribute, as you can see in the following link, where it is used to define default values for fields in the form: https://github.com/odoo/odoo/blob/8.0/addons/account/account_bank_view.xml#L42

By default, actions are not directly related to particular views (view `priority` is used to select a particular view to render), so you may think this approach has the limitation of not affecting any particular views.

You can, however, explicitly associate actions with particular Form/Tree views by creating additional `ir.actions.act_window.view` records. Please see the holidays module example where the same object `hr.holidays` is used to represent both leaves and allocations, having differentiated actions/forms/trees at the same time, as you can guess, leaves and allocations have different actions/contexts/forms/trees: https://github.com/odoo/odoo/blob/8.0/addons/hr_holidays/hr_holidays_view.xml#L235

I hope this helps.

Regards.

الصورة الرمزية
إهمال