如何在表单中为many2one字段使用过滤器?

0 投票
1 回答
549 浏览
提问于 2025-04-18 05:47

我在我的模块页面上有三个字段,其中两个是产品类别,另一个是产品。类别字段包含不同的类别。现在我想添加一个过滤器,这样我在类别字段中选择的类别,产品字段中只会列出那些类别的产品。这就像是一种排序或过滤。我对在表单视图中如何使用过滤器不是很了解。以下是我的代码.py:

class deg_form(osv.osv):
    _name = "deg.form"
    _columns = {
         'categ1':fields.many2one('product.category','Parent Category'),
         'categ2':fields.many2one('product.category','Child Category'),
         'my_products':fields.many2one('product.product','Products',size=64),

            }


deg_form()

这是它的xml:

<record id="mywin_form_view" model="ir.ui.view">
            <field name="name">mywin.form</field>
            <field name="model">deg.form</field>          
            <field eval="7" name="priority"/>
            <field name="arch" type="xml">
                <form string="FORM DATA" version="7.0">

                     <h1>
                       <label for="categ1" string="Parent category"/>
                           <field name="categ1" />
                     </h1>

                     <h1>
                       <label for="categ2" string="Child category"/>
                           <field name="categ2" />
                     </h1>
<newline/>

                     <h1> 
                       <label for="my_products" string="Products" domain = "[('categ1','=',True)]"/> 
                           <field name="my_products"/> 
                     </h1>


                 <button name="show_product" string="SHOW PRODUCT" type="action"/>
                 </form>
             </field>
    </record>

请帮我解决这个问题。

1 个回答

2

在py或xml中使用域过滤器如下:

domain = "[('categ_id','=',categ1)]"

或者

重写产品的搜索方法,并将类别字段作为参数传递:

def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
    if context is None:
        context = {}
    if context and context.get('search_default_categ_id', False):
        args.append((('categ_id', '=', context['categ_id'])))
    return super(product_product, self).search(cr, uid, args, offset=offset, limit=limit, order=order, context=context,count=count)

对于产品类别的父子关系,可以在py或xml中使用这个域过滤器(categ2):

domain = "[('parent_id','=',categ1)]"

撰写回答