如何显示/隐藏Filter,Group By选项中显示的字段?

时间:2018-03-08 10:31:30

标签: python odoo odoo-10 odoo-view

您好我正在尝试隐藏/显示Action(树视图)过滤器/分组依据字段选择部分中的一些字段。我找到了使用fields_get函数执行此操作的解决方案,但后来我遇到问题,当您在菜单项之间切换时,字段不会更新。我的解决方案是下面的。

@api.model
def fields_get(self, allfields=None, attributes=None):
    """Extend to hide fields from custom search."""
    # u'route_view': u'route_accounting'
    # Fieldai kuriuos reikia rodyti
    route_accounting_filter = ['last_adt', 'first_alt', 'carrier', 'cmr_receive_date',
                               'cmr_reg_no_1', 'cmr_req',
                               'cmr_send_date', 'send_seperate_documents',
                               'freight_customer', 'last_invoice_send_date',
                               'load_country_id', 'load_full_adr', 'last_relevant_invoice',
                               'payer', 'customer', 'other_po',
                               'purchase_order_route_loc', 'name', 'freight_so',
                               'trailer_id', 'truck_id', 'unload_country_id',
                               'unload_full_adr', 'billing_data', 'invoice_billing_no',
                               'cmr', 'cmr_copy_receive_date', 'debt',
                               'declaration', 'last_invoice_send_date', 'transportation_done',
                               'delivery_note', 'document_packet', 'documents_delayed_to_receive',

                               'transportation_done_date', 'last_invoice_send_date', 'cmr_reg_userr',
                               'cmr_reg_date', 'product_description'

                               ]

    route_accounting_group = ['name', 'last_adt', 'first_alt', 'carrier', 'cmr_reg_date'
                              'cmr_reg_userr', 'cmr_req', 'freight_customer', 'payer', 'load_country_id',
                              'trailer_id', 'truck_id', 'unload_country_id', 'product_route_id'
                              ]

    other_route_group = ['other_po_carrier', 'other_so_partner', 'other_po_description',
                         'other_po_truck', 'other_po_trailer', 'ferry_product', 'other_so_partner',
                         'other_po_carrier', 'other_po_description', 'other_po_truck', 'other_po_trailer',
                         'other_po_carrier_loading_time', 'other_po_carrier_unloading_time',
                         'last_relevant_bill_number'
    ]

    other_route_filter = ['name', 'other_so', 'other_po',
                         'vendor_reference_other', 'transportation_done', 'ferry_product', 'name'
                         ]

    res = super(RouteData, self).fields_get(
        allfields=allfields,
        attributes=attributes
    )

    if 'route_view' in self.env.context and self.env.context['route_view'] == 'route_accounting':
        print "ROUTE (ACCOUNTING VIEW))))"
        for field in res:
            if field not in route_accounting_filter:
                res[field]['selectable'] = False

        for field in res:
            if field not in route_accounting_group:
                res[field]['sortable'] = False

    elif 'route_view' in self.env.context and self.env.context['route_view'] == 'route_other':
        print "ROUTE (OTHER VIEW))))"
        for field in res:
            if field not in other_route_filter:
                res[field]['selectable'] = False

        for field in res:
            if field not in other_route_group:
                res[field]['sortable'] = False

    return res

我的观点如图所示。基本上每个菜单项都有自己的动作,但是来自同一个表,我相信这个原因即使我的解决方案检查上下文有效(它打印出他在不同的视图中)他也不会更新Group By中显示的字段/过滤器(甚至代码可以工作)。有什么东西我忘记了或者可能有不同的解决方案来轻松实现这一目标吗? (如果我不需要使用JS,会非常好)

enter image description here

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解这个问题。无论如何看看我的建议:

解决方案0

如果我理解得很好,你只想修改搜索视图。例如,如果某些搜索视图如下所示:

<record id="original_search_view" model="ir.ui.view">
    <field name="name">name.search</field>
    <field name="model">model.name</field>
    <field name="arch" type="xml">
        <search string="Partner Record">
            <field name="partner_id" string="Partner" filter_domain="[('partner_id','ilike',self)]"/>
            <field name="operation_key" string="Operation key" filter_domain="[('operation_key','ilike',self)]"/>
            <field name="partner_vat" string="Record" filter_domain="['|',('partner_vat','ilike',self),('partner_id','ilike',self)]"/>
            <separator/>

            <filter name="draft_filter" domain="[('state', '=', 'draft')]"/>

            <group expand="0" string="Group by..." >
                <filter string="Operation key" domain="[]" context="{'group_by' : 'operation_key'}"/>
            </group>
        </search>
    </field>
</record>

你可以通过像这样的继承来改变它

<record id="custom_search_view" model="ir.ui.view">
    <field name="name">custom.name.search</field>
    <field name="model">model.name</field>
    <field name="inherit_id" ref="module_name.original_search_view" />
    <field name="arch" type="xml">
        <!-- remves parnet_vat field from filters -->
        <field name="partner_vat" position="replace" />
        <filter name="draft_filter" position="replace" />

        <!-- removes group by filters -->
        <xpath expr="/search/group[1]" position="replace" />
    </field>
</record>

注意:小心删除项目。有时其他模块可以向原始视图添加更多字段,并且可能需要原始字段来获取位置。所以你可以让它们不可见,而不是完全删除它们。

解决方案1 ​​

您可以从头开始创建新的搜索视图,并使用search_view_id字段将其添加到操作窗口,如下所示:

<record model="ir.actions.act_window" id="credit_control_line_action">
    <field name="name">Credit Control Lines</field>
    <field name="type">ir.actions.act_window</field>
    <field name="res_model">model.name</field>
    <field name="domain"></field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="view_id" ref="custom_tree_view"/>

    <!-- filters selected by default, the format is: { 'search_default_[filter_name]': 1, }  -->
    <field name="context">{'search_default_filter_draft': 1, 'search_default_filter_to_be_sent': 1}</field>

    <!-- this search view is going to be used instead of the default view -->
    <field name="search_view_id" ref="custom_search_view"/>
</record>

注意:有关Odoo Documentation

上搜索视图的更多信息