1

I am using Odoo v10. While scanning a barcode, a string contains some characters of a char field value. For example,

  • A field value ('tracknum') = "20171103"
  • Search the field by entering a string "xxxxxx20171103" or "xxxx20171103yyy"

is there any way to do it? I have modified the search view :

<field name="tracknum" string="Tracknum" filter_domain="..."/>

enter image description here

How to dig out related records?

enter image description here

ChesuCR
  • 9,352
  • 5
  • 51
  • 114
cyliinhk
  • 75
  • 8
  • SearchView: ... any idea to change it? – cyliinhk Nov 05 '17 at 14:33
  • Do you mean in the Point of Sale module? – ChesuCR Nov 06 '17 at 01:30
  • not exactly. it should be used in general search. I input a string longer than the value stored in a field but it contains the word or characters. I hope to dig out records by the searching string. – cyliinhk Nov 06 '17 at 01:33
  • OK, check if the `name_search` method is what you are looking for. There are several examples in the source code. Although I haven't programme with the version 10 yet, so maybe the method is deprecated – ChesuCR Nov 06 '17 at 10:56
  • I use this in SearchView: But it only let me search by, e.g. "2017", and getting the record. I want I enter e.g. "xxx20171103zzz" to search out the record. Do you think is it possible? – cyliinhk Nov 07 '17 at 01:05
  • Ah! Now I understood. Maybe something like this? (I did not test it): `[('tracknum','in', self)]` – ChesuCR Nov 07 '17 at 08:48

1 Answers1

1

You can create an auxiliar computed field like this

custom_name = fields.Char(
    string='Custom',
    compute='_compute_custom_name',
    search='_search_custom_name'
)

@api.multi
@api.depends()
def _compute_custom_name(self):
    ''' The field has to be a computed field
        You do not need to do anything here
    '''
    pass    

def _search_custom_name(self, operator, value):
    ''' Actually this converts a domain into another one.
        With this new domain Odoo can search well

        Arguments:
            * operator: if you are searchig words it is going to be ilike
            * value:    the string ro search

        The method could return something like this
            * [('id', 'in', id_list)]
    '''

    all_records = self.search([])   # recordset with all the values of the current model
    ids = []
    if operator == 'ilike':            
        ids = all_records.filtered(lambda r: r.tracknum in value).mapped('id')


    return [('id', 'in', ids)]

Then you can add this field to the search view like this:

<field name="custom_name" string="Tracking Number" />

Keep in mind that it is not a stored field, so it is going to be very inefficient. And you should iterate over all the values each time you want to make a search.

Once you have added the field to the search view it shoul look like this, Tracking Number should appear in the field name

enter image description here

ChesuCR
  • 9,352
  • 5
  • 51
  • 114
  • 'in' is not a proper postgresql syntax. I try this but it fails with error. I check PostgreSQL has % for matching any sequence of characters. I use ('tracknum','in', '%'+self) it doesn't work but without error. any idea to improve? – cyliinhk Nov 08 '17 at 08:28
  • Ah, sure, `in` is only for lists then. Check [here](https://stackoverflow.com/questions/29442993/which-are-the-available-domain-operators-in-openerp-odoo) all the available operators. I thought that it would work as a python operator – ChesuCR Nov 08 '17 at 08:31
  • Check if my new answer helps please @cyliinhk. If the search method does not work maybe you should create a dummy compute field – ChesuCR Nov 08 '17 at 08:39
  • I have updated my answer, check if it helps you @cyliinhk – ChesuCR Nov 08 '17 at 21:01
  • thanks @ChesuCR. I will try first and give you feedback later. – cyliinhk Nov 08 '17 at 23:55
  • I apply above code together in SearchView as But it does not work (without error) and the result is nothing. I attach a screen cap for reference above. – cyliinhk Nov 10 '17 at 02:47
  • one more point for above example. when i search the correct no "1234", the record can be found. The method is working but only for identifying the exact string. – cyliinhk Nov 10 '17 at 03:00
  • The `filter_domain` attribute is not necessary anymore. Just add the field like this `` – ChesuCR Nov 10 '17 at 08:38
  • I get this error : AttributeError: 'stock.picking' object has no attribute '_search_custom_name' – cyliinhk Nov 12 '17 at 01:27
  • The field must be in the same model than the search view. Are you using the `stock.picking` on both sides? – ChesuCR Nov 13 '17 at 11:26
  • I have created an add-on module and _inherit = 'stock.picking' . No error but it doesn't give result e.g. "xx20171103" but if you type "20171103" a record is obtained. – cyliinhk Nov 21 '17 at 01:25
  • I have tested it in another model and it is working fine. Make sure you are searching by the field that you have added. I have added a image to the answer to be more clear – ChesuCR Nov 22 '17 at 18:11
  • Thanks. It finally works with this filter_domain attribute in SearchView and it works for C10/C11/E10/E11. – cyliinhk Nov 29 '17 at 10:10
  • Ah! I am glad to hear it :) – ChesuCR Nov 29 '17 at 11:46
  • oh i just figure out I get lose of my original though. Above works because I put a shorter string (e.g. 1245) than that of the value stored (X1234). As I initially quote, I need I input a longer string (e.g. ZX1234YYY) of input that contains a value of the field (i.e. 1234). Do you think is it possible? – cyliinhk Dec 01 '17 at 12:21
  • When you search by the custom field the `_search_custom_name` has to be executed. Try to put some logger to verify it – ChesuCR Dec 01 '17 at 13:39
  • @ChescuCR I am afraid I cannot figure out. To avoid error I have to use but it can search with the exact or short string. I attached the add-on module .py for reference (pls note "trackingnum" is used instead of "tracknum" here) – cyliinhk Dec 12 '17 at 14:00