How to set a new search filter? - odoo

How can I set default value to my filter button in search view ? I try to set a context to the action like the way it is in partner module.

I got this solution on another site
You can use only those fields which are defined in search view.
For this you need to pass proper key and value in context.key should like this
search_default_filter_name
Example: In res_partner search view there is one filter like this
<filter string="Persons" name="type_person" icon="terp-personal" domain="[('is_company','=',0)]"/>
If you want this filter as default pass context like this
<field name="context">{"search_default_customer":1,"search_default_type_person":1}</field>
If you don't want search_default_customer use {"search_default_type_person":1} instate of passing like this {"search_default_customer":0,"search_default_type_person":1}
You can also use field from search view
<field name="parent_id" filter_domain="[('parent_id','child_of',[self])]"/>
for use field pass context like this {"search_default_parent_id":1}

Related

How could I hide a field with a button in Odoo

Currently, I had a view form and there is some field that need to be hide or unhide by click a button.
I tried to use State, Boolean field but unable to achieve the result.
My question is could I do it on Odoo and how?
I used State and Boolean field and set by condition on field, however the result was not good.
When a user clicks a button, hidden field will appear back
Yes, you can do it.
Button action in odoo triggers write method before being executed.
You should be aware of this before implementing your logic and your domains in order to show/hide fields in XML Views.
Your need is to use invisible attribute applied to a domain inside your field.
invisible is normally static, but can be elasticised inside attrs attribute.
Another important thing is, field-based domains requires the field to be present in view in order to access it. A good practice is to make these fields invisible and put them on top of your view.
In your model:
show_fields = fields.Boolean(default=False)
field_to_show = fields.Char()
def show_fields_action(self):
for record in self: record.show_fields = True
In your view:
<field name="show_fields" invisible="1" />
<field name="field_to_show" attrs="{'domain': [('show_fields','=',True])}" />
<button name="show_fields_action" type="object" string="Show Fields" />
If your question UI/UX related, consider using notebooks instead.

How to add domain filter in odoo search view?

I want to add a custom search filter like this :
when I apply a filter like this is doesn't work
<filter string="Societe" name="company_id" domain='[("company_id","like","")]'/>
in my case, I want the condition is what the user enters on the search input input
It doesn't work because you shoud apply it on the name field.
domain='[("company_id.name","like","")]'
Actually you just need to add the field company_id as extension to a search view or just add it in your own search view:
<field name="company_id" />
And that's it. Now while typing a search term, the search view should show the user multiple search options.

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.

OpenERP. Add image into field dynamically in a tree view

I am using v7 and I want to show in a tree view a field with image icons (like a semaphore) depending of other field values in the same row.
Actually, I get the functionality I want with a function field and put the result as string but I really want it as an image. I don't know if is possible to return HTML from the function so I decided to do that with jQuery.
I implemented the jQuery code using the browser console and works but when I put the jQuery code in the view the "data-field" selector does not selected.
Please, can anyone explain me why or tell me another way to get my objective?
Lists
The root element of list views is tree. The list view's root can have the following attributes:
editable, default_order, colors, fonts, create, edit, delete, on_write, string
See more about ListView
You can achieve this by defining child elements button in list view.
I have added icon in product to show whether product is available / hold / sold based on product's status.
button
icon: icon to use to display the button
Xml code to display icon in listview.
<xpath expr="//notebook/page[#string='Order Lines']/field[#name='order_line']/tree[#string='Sales Order Lines']/field[#name='product_id']" position="before">
<field name="product_status" invisible="1" />
<button icon='Hold' readonly="1" attrs="{'invisible':[('product_status', '!=', 'hold')]}"/>
<button icon='Available' readonly="1" attrs="{'invisible':[('product_status', '!=', 'available')]}"/>
<button icon='sold' readonly="1" attrs="{'invisible':[('product_status', '!=', 'sold')]}"/>
</xpath>
NOTE
Remember base field on which you defined button's visibility must be
present on listview, doesn't matter whether it's visible or not but it
must be there, like product_status in above example.
base field must be store=True.
Why store=True ?
Reason: when you set any function field store=True then it will physically created in database, while you give this field name in
domain the odoo framework add this field directly in WHERE clause,
it will not access through browsable object, so if your field is
store=False then it won't be able to find that field and gives you an error.
icons must be present in web/static/src/img/icons.
if you want to keep icons in your custom module then you have to create the same hierarchy for that in side your module folder create /static/src/img/icons and keep all the icons there.
xpath is used to define at which location you want to add/update fields.
See more about xpath

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).