How does one link to other defined entities using their id? - odoo

When writing XML files I will occasionally need to reference another entity, such as a group, a category, or an action.
How can I accomplish this?

There are two different methods to do this, and which one you use depends on where you are in the record:
in the type="xml" or type="html" portions (such as tree and form views)
everywhere else
Inside the type=["xml" | "html"] portions you need to use %-interpolation:
<button string="..." name="%(fnx_pd.action_add_cleaning_order)d" type="action" />
<field name="item_id" domain="[('categ_id','=',%(fnx_pd.pd_cleaning)d)]" />
The thing you are linking to needs to be inside a %()d or %()s construct: %(module.id_name)d.
If not inside an xml or html segment, then you can use the OpenERP-provided ref() function to get the id:
<field name="value" eval="'ir.actions.server,' + str(ref('action_release'))"/>
<field name="context" eval="{'default_pos_categ_id': ref('point_of_sale.categ_others')}"/>
In both of the above methods, OpenERP will look up the actual value associated with the id given and substitute it into the record.

Related

how to make condition to a specific groups

I want to add a condition to one group access to no open and no create_edit just for normal people, but when I use the code below the admin have no access to edit I give to him all access right to file security
<field name="name" domain="[('person_id', '=', person_id), ]"
options='{"no_open":True,"no_create_edit": True}' readonly="1"
/>
No way to get it done without Odoo core extension to provide that features or by overriding the method fields_view_get to dynamically change the view definition based on the user groups.

How to use 'like' operator in the attrs attribute to hide or show a button

I need to hide or show a button depending on whether or not a Char field has a specific string in it. It seems that the 'like' operator would be perfect. In my xml, I have:
<record model="ir.ui.view" id="my_view">
<field name="name">my.form</field>
<field name="model">mymodule.my</field>
<field name="arch" type="xml">
<form string="My Form">
<header>
<button name="test_like_1" type="object"
string="Should not appear"
attrs="{'invisible':[('state2','like','measure')]}"
/>
<button name="test_like_2" type="object"
string="Should appear"
attrs="{'invisible':[('state2','not like','measure')]}"
/>
...
State2 contains 'measure,prelien', so I expect that the first button will be invisible and the second visible. However, both are invisible.
What am I missing?
Edit
I ran the query that I think Odoo would create from this domain -
select id, description, state2 from mymodule_my where state2 like '%measure%';
It runs as expected, returning the records that have "measure" as a substring. Somehow, this SQL isn't being generated/used. My next step is to dig through the Odoo code & see what's happening.
Can anyone provide insight to what's going on?
I found the problem - available operators for attrs in a view describes it best and outlines one possible solution. The gist is that the domains specified in attrs are evaluated in javascript on the client. The 'like' & 'ilike' operators aren't implemented.
You can verify this by viewing the console. In my case, I got a ton of these warnings -
Unsupported operator ilike in domain [["state2","ilike","measure"]]
I'm looking into either extending the compute_domain function in odoo/addons/web/static/src/js/framework/data.js as suggested1 or simply working-around the limitation.
You could try
attrs="{'invisible':[('state2','in',['Measure','MEASURE','measure'])]}"
And
attrs="{'invisible':[('state2','not in',['Measure','MEASURE','measure'])]}"
You may have to add more items to your lists. I am not sure if like and not like are supported but this is a method I see used in other addons.
You're comparing the string 'state2' instead of the value of the field state2 also you should be comparing the other way around, this isn't exactly the best example but you should get what i mean.
>>> 'measure,prelien' in 'measure'
False
>>> 'measure' in 'measure,prelien'
True
>>>
The first condition will never evaluate to true. you should do this instead
define a char field named default, and set it's default value to 'measure' and make it hidden on the view
measure = fields.Char('Measure', default='measure', store=False)
Then your view should look like this
<field name="measure" invisible="1" />
<button name="test_like_1" type="object"
string="Should not appear"
attrs="{'invisible':[('measure', 'like', state2)]}"
/>
<button name="test_like_2" type="object"
string="Should appear"
attrs="{'invisible':[('measure', 'not like', state2)]}"
/>

Is it possible to change relation of a field based on a condition?

Suppose there is a field 'A' and has an fields declaration say
A:fields.many2one('new.new')
and then based on any condition, the same field should get a different relation like
if condition:
A:fields.many2one(old.old)
Is it possible?
AFAIK the answer is No. You cannot change at runtime the comodel of a m2o field. It would require to change the Foreign Key and other IR relations which are created when the module is installed/updated.
Maybe you can achieve something similar by creating both
A: fields.many2one('old.old')
B: fields.many2one('new.new')
C: fields.Boolean()#condition, eventually computed
and then using A or B depending on your condition.
for the view part:
<field name="C" invisible="1"/>
<field name="A" attrs="{'invisible': [('C','=',False)]}"/>
<field name="B" attrs="{'invisible': [('C','=',True)]}"/>
C is always invisible, A and B visibility depends on C

How can I pass a context (in a view, in a field tag) from another field's value?

I want to create a many2one field which, in view, passes a context:
<field name="my_m2o_field" context="{'foo': 'bar'}" />
The goal here is to affect the related view (i.e. When you click on "Create and Edit" in a dropdown, you get a popup rendered by the related object's current view).
Such field tag works as expected if, in context, I have something like "{'default_code': 'my.code'}", provided code field exists in the related object.
However, the context I actually need is too large (20 entries), and I have to generate 5 contexts like that (with a minor diference, since I have 5 similar fields).
I would like to wrap the context in a -non-storable- functional field (I'd need, actually, 5 similar functional fields), and pass such context as value for the context attribute:
<field name="my_context_field" invisible="1" />
<field name="my_m2o_field" context="my_context_field" />
Is it possible? What type should I use (type= argument in the function constructor).

ContentTypeRef not working as expected, inheriting from System instead of mine

I have a SP project with two features:
a first feature that defines some fields, a content type and a list definition
a second feature that defines a list instance of the first feature definition
in the second feature, I use the ContentTypeRef element to bind to the content type defined in the first feature. I saw in many blog post and forum thread that Fields are not correctly populated to the list, but it's not my issue (maybe it's related ?)
The instantiated list defines a content type, but instead of inheriting from my content type, it inherits the "System" content type.
Is this behavior correct ? how can I actually inherits my content type instead of system content type ?
thx in advance
[Edit] the simpliest workaround I found is to copy past the content type definition into the contenttypes element of my list schema... but it's still a copy/paste operation (as ugly as it can be)
Please make sure that your content type ID is valid, I never managed to bypass the item content type (0x01) which means that your content type will have an ID of 0x0100{A-GUID}.
Anyway, even if you defined properly your content type and this one is working as expected when you bind it to a custom list, you'll still need to re-declare it in your list schema with all its field reference and once again, copy most of the definition of your field (I had issue with less than ID, name, display name, type in this area)...
Eg with the last list I created :
<ContentTypes>
<ContentType ID="0x0100FDCCBFFB0FBF4D5C8E069F582412909602" Name="UniverseTranslation" Group="XYZ" Description="Universe Translation" Version="0">
<FieldRefs>
<FieldRef ID="{39BF387B-C20A-4D30-BD17-CB70E4609FA2}" Name="LookupUniverse" DisplayName="Universe" Required="TRUE" />
<FieldRef ID="{824F7063-6D09-48CD-B5BA-FE9B5EE36D6A}" Name="WCC_Language" DisplayName="Language" Required="TRUE" />
<FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" DisplayName="Translation" Required="TRUE" />
<FieldRef ID="{EC8E4DB7-B715-430B-9B4A-F222F025EFAB}" Name="RichDescription" DisplayName="Description"/>
</FieldRefs>
</ContentType>
</ContentTypes>
<Fields>
<Field
ID="{39bf387b-c20a-4d30-bd17-cb70e4609fa2}"
Name="LookupUniverse"
DisplayName="Universe"
Type="Lookup"
ShowField="Title"
Required="TRUE"
EnforceUniqueValues="FALSE"
List="Lists/Universes">
</Field>
<Field
ID="{824F7063-6D09-48CD-B5BA-FE9B5EE36D6A}"
Name="WCC_Language"
DisplayName="Language"
Type="VariationLabelsFieldType"
Required="TRUE">
</Field>
<Field
ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}"
Name="Title"
DisplayName="Translation"
Type="Text"
Required="TRUE">
</Field>
<Field
ID="{EC8E4DB7-B715-430B-9B4A-F222F025EFAB}"
Name="RichDescription"
DisplayName="Rich Description"
Type="Note"
NumLines="4"
RichText="TRUE"
RichTextMode="Compatible"
AllowHyperlink="TRUE"
IsolateStyles="FALSE"
AppendOnly="FALSE"
Required="FALSE">
</Field>
</Fields>
If you can post your content type definition and part of your list schema, I'm pretty sure we'll be able to provide a more relevant help.
Kindly
It worked for me by adding the relative folder path for the content type's resource folder. It also worked for adding multiple references for content types to the list as follows:
<ContentTypes>
<ContentType ID="0x01006775E96C04A04F52AC1FCE50F0CB0901" Name="contentType1" Group="Test Content Types" Description="Test Content Type" Inherits="TRUE" Version="0">
<Folder TargetName="contentType1" />
</ContentType>
<ContentType ID="0x0100958BB07B626A494F9201B03E96948F3D" Name="contentType2" Group="Test Content Types" Description="Test Content Type" Inherits="TRUE" Version="0">
<Folder TargetName="contentType2" />
</ContentType>
</ContentTypes>