Skip to Content
Menu
This question has been flagged
1 Reply
10252 Views

I am using Odoo v.11.

I was trying to customize the search bar for my model, so that the user can look for a string matching the name or the description:

        <record model="ir.ui.view" id="course_search_view">
<field name="name">course.search</field>
<field name="model">openacademy.course</field>
<field name="arch" type="xml">
<search>
<field name="name"/>
                    <field name="description"/>
</search>
</field>
</record>


This seems to work, but the user has to specify if he's looking for the name or the description. Instead, I'd like that the user could search for a string, and the software should return every record matching that string in the name or description.

My idea was to define a computed field in the model, as the simple concatenation of field+" "+description:

    
text_data = fields.Char(string="Textual data", compute='_text_data')

    @api.depends('name', 'description')
def _text_data(self):
for r in self:
r.text_data = str(r.name)+" "+str(r.description)

...and setup the search box to look for that field:

        <record model="ir.ui.view" id="course_search_view">
<field name="name">course.search</field>
<field name="model">openacademy.course</field>
<field name="arch" type="xml">
<search>
<field name="text_data"/>
</search>
</field>
</record>


But this does not appear to work: the search always succeed, and all the records are returned even if the search string appears nowhere in the names and descriptions...

Am I doing something wrong?

Thanks in advance for any help.






Avatar
Discard
Best Answer

This is possible without needing to create a new field.

This is how Sales Order search works, searching for Sales Order Name, Client Order Reference or Customer:

<field name="name" 
string="Sales Order"
filter_domain="['|','|',('name','ilike',self),('client_order_ref','ilike',self),('partner_id','child_of',self)]"/>

Just put something similar in your XML file - "self" represents what the user enters.

https://github.com/odoo/odoo/blob/11.0/addons/sale/views/sale_views.xml#L431

Avatar
Discard
Author

This looks great, thank you very much!

I also keep forgetting that domains are ORM objects and things like ('partner_id.name' , 'ilike' , self) can also be done. Love the way the ODOO search works!!!

Related Posts Replies Views Activity
0
Nov 18
2518
1
Feb 25
440
0
Sep 23
1512
2
Jun 23
3126
1
Aug 22
11719