I am trying to make a button in an Odoo view invisible when two conditions are both met.I am able to make the field invisible when either of the conditions are met like this:
attrs="{'invisible': [('status', '!=', 'validated'),('api_result', '!=', 'Valid')]}"
I want the button to be visible only when status == "validated" && api_result == "Valid"
But when I try to introduce the "&" I get an error. There's a long traceback which I'm not attaching now because I assume there is something wrong with my syntax:
attrs="{'invisible': ['&',('status', '!=', 'validated'),('api_result', '!=', 'Valid')]}"
I admit that I find the syntax 'challenging' and haven't been able to find any clear documentation, so I would appreciate a pointer.
You can do that using or as below:
attrs="{'invisible': ['|',('status', '!=', 'validated'),('api_result', '!=', 'Valid')]}"
Related
I found this code in the odoo and the options="{'model': 'crm.lead'} confused me?
What does this mean? And when should I use it?
<field name="assignment_domain" string="Domain" widget="domain"
options="{'model': 'crm.lead'}"
attrs="{'invisible': ['|', ('assignment_max', '=', 0), ('assignment_optout', '=', True)]}"/>
The Domain field allows the user to construct a technical-prefix domain and see the selected records in real-time.
The model option is used to set the model the field must work with, the model and domain are used to fetch records.
I want to make invisible a field if one of those options are true, I don't know if this is possible.
I try:
<field name="x_field1" string="something" attrs="{'invisible': [('x_field2','!=','value'),'|',('x_field3','=','value'),'|',('x_field4','=','value')]}"/>
And this:
<field name="x_field1" string="something" attrs="{'invisible': ['|',('x_field2','!=','value'),('x_field3','=','value'),('x_field4','=','value')]}"/>
Without success.
From Odoo Domains documentation:
'|'logical OR, arity 2.
You have three options, so you need to use two | operators, like the following:
['|', '|', ('x_field2', '!=', 'value'), ('x_field3', '=', 'value'), ('x_field4', '=', 'value')]
I have one field which i want to hide when specific journal selected.
<field name="any_field" attrs="{'invisible':[('journal_id','=',10])]}"/>
And For that above code is working fine.
I sure it is not a way to check condition.So, i tried this way.
<field name="any_field" attrs="{'invisible':[('journal_id','=',ref('my_module.account_journal_10'))]}"/>
It's working then i tried by using the static field on the journal eg. code.
<field name="any_field" attrs="{'invisible':[('journal_id.code','=','CARD')]}"/>
But still not working and getting error from view.
I am thinking if i can return the attrs from .py like i do for domain.
eg.
return {'domain':
{
'any_field':[('journal_id','=',self.env.ref('my_module.account_journal_10').id)]
}
}
Can anybody help me in this?
Thank you.
As far as i know these attrs domains/filters are client-side so you can't use something like journal_id.code or partner_id.customer, because the client doesn't know about such data.
A possible workaround is to define a related field on the model you're trying to do this. Let's assume the model is my.model and already has this Many2one field journal_id:
journal_code = fields.Char(string="Journal Code", related="journal_id.code")
Now you need to extend the view of my.model:
<field name="journal_code" invisible="1" />
<field name="any_field" attrs="{'invisible':[('journal_code','=','CARD')]}"/>
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)]}"
/>
Im having problems with creating a filter on stock.picking object. Just recently i build a simple "privilege relay" - in each stock location you can define "Assigned User Group", thanks to that users that are in particular group can or cannot confirm moves for or out of the location.
Stock.picking:location_id -> assigned_user_group -> users
Now I would like to create a filter (later to be set default) on stock picking tree view that will show only the moves which locations (source location and destination location; i use them in stock.picking object) can be managed by a viewing user.
By far I wrote a filter that looks like that:
<record id="view_picking_internal_search_pl" model="ir.ui.view">
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.view_picking_internal_search"/>
<field name="arch" type="xml">
<filter icon="terp-dialog-close" name="done" string="Done" domain="[('state','=','done')]" help="Pickings already processed" position="after">
<filter icon="terp-check" name="locgroup" string="Location Group" domain="[('user_id','in',[user.id for user in location_id.user_group.users])]" context="{'group_by':'date'}"/>
</filter>
</field>
</record>
I also added field location_id to the tree view.
But Im still getting an error (after choosing the filter) that even google doesnt know anything about:
TypeError: results.group_by is undefined
My questions are:
By looking on domain in filter field - what am i doing wrong?
Is something like that even possible?
I will gladly welcome any help.
Firstly, i think your domain is not correct, it could have been :
[('user_group.users.id', '=', uid)]
(because the first element of the tuple is a field on the model; and uid is a special value supplied in search views)
Next, This error :
TypeError: results.group_by is undefined
Seems to be a Javascript Error (coming from openerp-web interface), it often throws error like that when it receives unexpected values (when we make a mistake defining a view for example).
can you tell us if using the domain above solved your problem ?
NB: does your field user_group is a required field ? If not, i think the domain above won't display picking where user_group is not set, if you want to display picking where user_group is not set too, you can set a domain like that:
['|',('user_group.users.id', '=', uid), ('user_group','=',False)]
Regards