How to include a search filter by sales invoice period group ( get default value of Selection Field ) Tryton v5.2

Hi. I have this following module : https://github.com/NaN-tic/trytond-sale_invoice_grouping_period/blob/5.2/party.py , wich i want to make sale_invoice_grouping_period field searchable inside tree view of parties. Here is my code:

from trytond.pool import PoolMeta
from trytond.model import ModelSQL, fields
from trytond.pool import Pool

class Party(metaclass=PoolMeta):
    'Party Sales Period Searcher'
    __name__ = 'party.party'

    sale_period = fields.Function(fields.Char('Sale Periods', help="This is a test"),
        getter='get_periods',
        searcher='searcher_sale_period')

    @classmethod
    def searcher_sale_period(cls, name, clause):
        return [
            ('party.sale_period', clause[1])
        ]

    def get_periods(self, name):
        p = Pool()
        SaleMethod = p.get('party.party.sale_invoice_grouping_method')
        if SaleMethod.sale_invoice_grouping_method:
            return SaleMethod.sale_invoice_grouping_period  **# How to get the field value ???**
        return None

My party.xml defined like this:

        <?xml version="1.0"?>
        <tryton>
              <data>
                <record model="ir.ui.view" id="party_sales_period_list">
                    <field name="model">party.party</field>
                    <field name="inherit" ref="party.party_view_tree"/>
                    <field name="name">party_period_list</field>
                </record>
              </data>
        </tryton>

my list view

        <?xml version="1.0"?>
        <data>
            <xpath expr="/tree/field[@name='code']" position="after">
                <field name="sale_period"/>
            </xpath>
        </data>

I get the following error:

Traceback (most recent call last):
  File "/trytond-5.2.20-py3.6.egg/trytond/wsgi.py", line 104, in dispatch_request
    return endpoint(request, **request.view_args)
  File "/trytond-5.2.20-py3.6.egg/trytond/protocols/dispatcher.py", line 48, in rpc
    request, database_name, *request.rpc_params)
  File "/trytond-5.2.20-py3.6.egg/trytond/wsgi.py", line 72, in auth_required
    return wrapped(*args, **kwargs)
  File "/trytond-5.2.20-py3.6.egg/trytond/protocols/wrappers.py", line 131, in wrapper
    return func(request, pool, *args, **kwargs)
  File "/trytond-5.2.20-py3.6.egg/trytond/protocols/dispatcher.py", line 217, in _dispatch
    response = app.make_response(request, result)
  File "/trytond-5.2.20-py3.6.egg/trytond/wsgi.py", line 123, in make_response
    response = cls.response(data, request)
  File "/trytond-5.2.20-py3.6.egg/trytond/protocols/jsonrpc.py", line 160, in response
    response, cls=JSONEncoder, separators=(',', ':')),
  File "/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/trytond-5.2.20-py3.6.egg/trytond/protocols/jsonrpc.py", line 61, in default
    return marshaller(obj)
  File "/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'Selection' is not JSON serializable

I understand that JSON is a dictionary, and what i want to get is inside a list of tuples. I think i’m quite wrong about how i should do this… Can someone help me please ? :slight_smile:

Database frame: i do have stored the following lines :

 id  | party | sale_invoice_grouping_method | sale_invoice_grouping_period 
-----+-------+------------------------------+------------------------------
   1 |   178 | standard                     | weekly-0
   2 |   179 | standard                     | weekly-0
   3 |   180 | standard                     | monthly
   4 |   181 | standard                     | daily
   5 |   182 | standard                     | weekly-0
   6 |   183 | standard                     | weekly-0
   7 |   184 | standard                     | weekly-0
   8 |   185 | standard                     | weekly-0
   9 |   186 | standard                     | weekly-0
  10 |   187 | standard                     | weekly-0
  11 |   188 | standard                     | monthly

This is a Multivalue field which do not implement a searcher by default. You should implement a searcher which takes in account the values from the context to pick the right record.

A similar case has been discussed recently on this forum.

Hope it helps!

1 Like

@pokoli thanks for the tip ! I am back on this… and searchig for examples to see how it’s been implemented in other scenarios… gonna check again the similar case. If you have any more suggestions or anyone else reading this post please share by leave a comment :slight_smile:

Sorry ! I have been completely wrong where i should place the filter :smiley: Just received the other day proper instructions about where it should be placed and now it works, but i just need to translate what i’m showing and implement the filter function as the error that shows a message like "NotImplementedError: Missing the search function of field “Período de agrupación de facturas de ventas” (“Sale Invoice Grouping Period”).

from trytond.pool import PoolMeta
from trytond.model import ModelSQL, fields
from trytond.pool import Pool

class SaleAccountInvoiceGroupingMethod(metaclass=PoolMeta):
    'Party Sales Period Searcher and tree view'
    __name__ = 'account.invoice'

    sale_period = fields.Function(fields.Char('Sale Period', help="This is a test"), **# how to translate help tool in local .po ?** 
        getter='get_periods',
        searcher='searcher_sale_period')

    @classmethod
    def searcher_sale_period(cls, name, clause):
        return [
            ('party.sale_invoice_grouping_period',  clause[1], clause[2])
        ]

    def get_periods(self, name):
        if self.party.sale_invoice_grouping_method == 'standard':
            return self.party.sale_invoice_grouping_period
        return 'None'

The thing is that i’m seeing the default language and not the translatiosn for each period. Seen this post and it looks that it should be done like this ?If somebody can explain me a little bit more how this works if so ? https://discuss.tryton.org/t/how-to-get-fields-translations/2339?u=daniel_dev

And thought that if i’m allready doing thranslatios for Sale Peridos i also should do the same for “Payment Days” (i’ve just created a pull request on git-hub- trytond-account_payment_days ) as it’s not translated. Any help would be appreciated!