How-to create filtered views with `context_domain` - e.g. reporting date

If users often use the same kind of filters, it might me a good idea to create an extra filtered view. This is especially true it there a many filter criterias or if expressing the criteria in the search field is complicates.

Examples in Tryton 7.0 are:

  • Sales / Products allows to filter the products available for sale by different criteria, e.g the warehouse

  • With module product_price_list_dates, price list lines get a start and end date. In XXX, when clicking “Open Lines” you can filter the lines which are valid at some date.

    Price list lines valid at some example date

There are many more examples e.g. selecting the reporting date of association members or the reporting period in accounting.

How to add a Filter

  1. Create a Model defining the filter criteria. Example for filtering a reporting date:
class MemberContext(ModelView):
    "Member Context"
    __name__ = 'association.member.context'

    date = fields.Date("Date")

    @classmethod
    def default_date(cls):
        pool = Pool()
        Date = pool.get('ir.date')
        return Date.today()
  1. Mind registering the Model in __init__.py

  2. Add some lines to you existing view. Like this:

<record model="ir.action.act_window" id="act_members_form">
    <field name="name">Members</field>
    <field name="res_model">association.member</field>
    <!-- next to fields are added for the filter -->
    <field name="context_model">association.member.context</field>
    <field name="context_domain"
        eval="[['OR', ('join_date', '=', None), ('join_date', '&lt;=', Eval('date', Date()))],
               ['OR', ('leave_date', '=', None), ('leave_date', '&gt;=', Eval('date', Date()))]]"
        pyson="1"/>
</record>

The context_model is what provides the filter data and context_domain defines additional filter criteria. The context_domain is required here since the Member has not date (as coming from MemberContext), but date must be within join_date and leave_date.

If you wonder how the filter in ‘Sale / Product’ looks like: This is the context Model and this the context_domain.

1 Like