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:

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