[软件设计/软件工程] OPENERP学习笔记 DOMAIN 的应用

[复制链接]
发表于 2022-5-2 13:23:56
1.在Action中定义,domain用于对象的默认搜索条件:

例子:
  1.         <record id="action_orders" model="ir.actions.act_window">
  2.             <field name="name">Sales Orders</field>
  3.             <field name="type">ir.actions.act_window</field>
  4.             <field name="res_model">sale.order</field>
  5.             <field name="view_type">form</field>
  6.             <field name="view_mode">tree,form,calendar,graph</field>
  7.             <field name="search_view_id" ref="view_sales_order_filter"/>
  8.             <field name="context">{}</field>
  9.             <field name="domain">[('state','not in',('draft','sent','cancel'))]</field>
  10.             <field name="help" type="html">
  11.               <p class="oe_view_nocontent_create">
  12.                 Click to create a quotation that can be converted into a sales
  13.                 order.
  14.               </p><p>
  15.                 OpenERP will help you efficiently handle the complete sales flow:
  16.                 quotation, sales order, delivery, invoicing and payment.
  17.               </p>
  18.             </field>
  19.         </record>
复制代码


在:

  1.    <field name="domain">[('state','not in',('draft','sent','cancel'))]</field>
复制代码


定义为在打开订单窗口时只搜索不在三种状态('draft','sent','cancel')的订单。



2、定义在对象(或视图)的关联字段(many2one和many2many类型字段)中,字段值为关联表的id,域用于过滤关联表的记录:

例子:
  1.   _name = 'sale.order.line'
  2.     _description = 'Sales Order Line'
  3.     _columns = {
  4.         'order_id': fields.many2one('sale.order', 'Order Reference', required=True, ondelete='cascade', select=True, readonly=True, states={'draft':[('readonly',False)]}),
  5.         'name': fields.text('Description', required=True, readonly=True, states={'draft': [('readonly', False)]}),
  6.         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of sales order lines."),
  7.         'product_id': fields.many2one('product.product', 'Product', domain=[('sale_ok', '=', True)], change_default=True),
  8.         'invoice_lines': fields.many2many('account.invoice.line', 'sale_order_line_invoice_rel', 'order_line_id', 'invoice_id', 'Invoice Lines', readonly=True),
  9.         'invoiced': fields.function(_fnct_line_invoiced, string='Invoiced', type='boolean',
  10.             store={
  11.                 'account.invoice': (_order_lines_from_invoice, ['state'], 10),
  12.                 'sale.order.line': (lambda self,cr,uid,ids,ctx=None: ids, ['invoice_lines'], 10)}),
  13.         'price_unit': fields.float('Unit Price', required=True, digits_compute= dp.get_precision('Product Price'), readonly=True, states={'draft': [('readonly', False)]}),
  14.         'type': fields.selection([('make_to_stock', 'from stock'), ('make_to_order', 'on order')], 'Procurement Method', required=True, readonly=True, states={'draft': [('readonly', False)]},
  15.          help="From stock: When needed, the product is taken from the stock or we wait for replenishment.\nOn order: When needed, the product is purchased or produced."),
  16.         'price_subtotal': fields.function(_amount_line, string='Subtotal', digits_compute= dp.get_precision('Account')),
  17.         'tax_id': fields.many2many('account.tax', 'sale_order_tax', 'order_line_id', 'tax_id', 'Taxes', readonly=True, states={'draft': [('readonly', False)]}),
  18.         'address_allotment_id': fields.many2one('res.partner', 'Allotment Partner',help="A partner to whom the particular product needs to be allotted."),
  19.         'product_uom_qty': fields.float('Quantity', digits_compute= dp.get_precision('Product UoS'), required=True, readonly=True, states={'draft': [('readonly', False)]}),
  20.         'product_uom': fields.many2one('product.uom', 'Unit of Measure ', required=True, readonly=True, states={'draft': [('readonly', False)]}),
  21.         'product_uos_qty': fields.float('Quantity (UoS)' ,digits_compute= dp.get_precision('Product UoS'), readonly=True, states={'draft': [('readonly', False)]}),
  22.         'product_uos': fields.many2one('product.uom', 'Product UoS'),
  23.         'discount': fields.float('Discount (%)', digits_compute= dp.get_precision('Discount'), readonly=True, states={'draft': [('readonly', False)]}),
  24.         'th_weight': fields.float('Weight', readonly=True, states={'draft': [('readonly', False)]}),
  25.         'state': fields.selection([('cancel', 'Cancelled'),('draft', 'Draft'),('confirmed', 'Confirmed'),('exception', 'Exception'),('done', 'Done')], 'Status', required=True, readonly=True,
  26.                 help='* The \'Draft\' status is set when the related sales order in draft status. \
  27.                     \n* The \'Confirmed\' status is set when the related sales order is confirmed. \
  28.                     \n* The \'Exception\' status is set when the related sales order is set as exception. \
  29.                     \n* The \'Done\' status is set when the sales order line has been picked. \
  30.                     \n* The \'Cancelled\' status is set when a user cancel the sales order related.'),
  31.         'order_partner_id': fields.related('order_id', 'partner_id', type='many2one', relation='res.partner', store=True, string='Customer'),
  32.         'salesman_id':fields.related('order_id', 'user_id', type='many2one', relation='res.users', store=True, string='Salesperson'),
  33.         'company_id': fields.related('order_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
  34.     }
复制代码

在:

  1. 'product_id': fields.many2one('product.product', 'Product', domain=[('sale_ok', '=', True)], change_default=True),
复制代码


定义关联的选择产品时,仅显示可供销售的产品。



3.在搜索视图中定义,域用于自定义搜索条件:

例子:

   
  1.    <record id="view_sales_order_filter" model="ir.ui.view">
  2.             <field name="name">sale.order.list.select</field>
  3.             <field name="model">sale.order</field>
  4.             <field name="arch" type="xml">
  5.                 <search string="Search Sales Order">
  6.                     <field name="name" string="Sales Order" filter_domain="['|',('name','ilike',self),('client_order_ref','ilike',self)]"/>
  7.                     <filter icon="terp-mail-message-new" string="Unread Messages" name="message_unread" domain="[('message_unread','=',True)]"/>
  8.                     <separator/>
  9.                     <filter icon="terp-document-new" string="Quotations" name="draft" domain="[('state','in',('draft','sent'))]" help="Sales Order that haven't yet been confirmed"/>
  10.                     <filter icon="terp-check" string="Sales" name="sales" domain="[('state','in',('manual','progress'))]"/>
  11.                     <filter icon="terp-dolar_ok!" string="To Invoice" domain="[('state','=','manual')]" help="Sales Order ready to be invoiced"/>
  12.                     <filter icon="terp-dolar_ok!" string="Done" domain="[('state','=','done')]" help="Sales Order done"/>
  13.                     <separator/>
  14.                     <filter string="My Sales Orders" domain="[('user_id','=',uid)]" help="My Sales Orders" icon="terp-personal" name="my_sale_orders_filter"/>
  15.                     <field name="partner_id" filter_domain="[('partner_id', 'child_of', self)]"/>
  16.                     <field name="user_id"/>
  17.                     <field name="project_id"/>
  18.                     <group expand="0" string="Group By...">
  19.                         <filter string="Customer" icon="terp-personal" domain="[]" context="{'group_by':'partner_id'}"/>
  20.                         <filter string="Salesperson" icon="terp-personal" domain="[]" context="{'group_by':'user_id'}"/>
  21.                         <filter string="Status" icon="terp-stock_effects-object-colorize" domain="[]" context="{'group_by':'state'}"/>
  22.                         <filter string="Order Date" icon="terp-go-month" domain="[]" context="{'group_by':'date_order'}"/>
  23.                     </group>
  24.                </search>
  25.             </field>
  26.         </record>
复制代码


在:

  1. <field name="name" string="销售订单" filter_domain="['|',('name','ilike',self),('client_order_ref','ilike',self)]"/>
复制代码


定义为在搜索视图中输入订单号时同时按订单号和客户关联号进行过滤。

<filter string="我的销售订单" domain="[('user_id','=',uid)]" help="我的销售订单" icon="terp-personal" name="my_sale_orders_filter"/>

在搜索视图中选择“我的销售订单”选项卡以过滤销售人员是当前登录用户的订单时定义。



4、在记录规则中定义,domain用于定义用户对对象中记录的访问权限:

例子:
  1.     <record model="ir.rule" id="sale_order_comp_rule">
  2.         <field name="name">Sales Order multi-company</field>
  3.         <field name="model_id" ref="model_sale_order"/>
  4.         <field name="global" eval="True"/>
  5.         <field name="domain_force">['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])]</field>
  6.     </record>
复制代码

在:

<field name="domain_force">['|',('company_id','=',False),('company_id','child_of',[user.company_id.id.id])]</field>

定义只允许用户查询订单中未指定的销售订单或订单中指定的分公司有访问权限。



5.领域表达规则说明:

域中的单个条件是一个三元素元组。
第一个是对象的一列,即字段名;
二是比较运算符“=, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right”;
第三个是用于比较的值。
多个条件用“|”链接 (或)、“&”(和)、“!” (无)逻辑运算符。
逻辑运算符作为前缀放在条件之前。 “|” 而“&”必须与两个条件相连,而“!” 否定一个条件。 默认逻辑运算符是“&”。


例如:
['|', ('project_id.privacy_visibility', '=', 'public'), '&', ('project_id.privacy_visibility', 'in', ['portal', 'followers']), '|', ('message_follower_ids','in', [user.partner_id.id]), ('user_id', '=', user.id), ]

写个容易看的方式:

[
'|',('project_id.privacy_visibility', '=', 'public'),

  '&',('project_id.privacy_visibility', 'in', ['portal', 'followers']),

      '|', ('message_follower_ids','in', [user.partner_id.id]),
      ('user_id', '=', user.id),
]


这个例子的意思是:

(('project_id.privacy_visibility', '=', 'public') or
(('project_id.privacy_visibility', 'in', ['portal', 'followers']) and (('message_follower_ids','in', [user.partner_id.id]) or ('user_id', '=', user.id))))

(1. Defined in action, domain is used for the default search criteria of the object:
example:
<record id="action_orders" model="ir.actions.act_window">
<field name="name">Sales Orders</field>
<field name="type">ir. actions. act_ window</field>
<field name="res_model">sale. order</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form,calendar,graph</field>
<field name="search_view_id" ref="view_sales_order_filter"/>
<field name="context">{}</field>
<field name="domain">[('state','not in',('draft','sent','cancel'))]</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click to create a quotation that can be converted into a sales
order.
</p><p>
OpenERP will help you efficiently handle the complete sales flow:
quotation, sales order, delivery, invoicing and payment.
</p>
</field>
</record>
At:
<field name="domain">[('state','not in',('draft','sent','cancel'))]</field>
It is defined as only searching for orders not in three statuses ('draft ',' sent ',' Cancel ') when opening the order window.
2. Defined in the associated fields (many2one and many2anytype fields) of the object (or view), the field value is the ID of the associated table, and the field is used to filter the records of the associated table:
example:
_ name = 'sale. order. line'
_ description = 'Sales Order Line'
_ columns = {
'order_ id': fields. many2one('sale.order', 'Order Reference', required=True, ondelete='cascade', select=True, readonly=True, states={'draft':[('readonly',False)]}),
'name': fields. text('Description', required=True, readonly=True, states={'draft': [('readonly', False)]}),
'sequence': fields. integer('Sequence', help="Gives the sequence order when displaying a list of sales order lines."),
'product_ id': fields. many2one('product.product', 'Product', domain=[('sale_ok', '=', True)], change_ default=True),
'invoice_ lines': fields. many2many('account.invoice.line', 'sale_order_line_invoice_rel', 'order_line_id', 'invoice_id', 'Invoice Lines', readonly=True),
'invoiced': fields. function(_fnct_line_invoiced, string='Invoiced', type='boolean',
store={
'account. invoice': (_order_lines_from_invoice, ['state'], 10),
'sale. order. line': (lambda self,cr,uid,ids,ctx=None: ids, ['invoice_lines'], 10)}),
'price_ unit': fields. float('Unit Price', required=True, digits_compute= dp.get_precision('Product Price'), readonly=True, states={'draft': [('readonly', False)]}),
'type': fields. selection([('make_to_stock', 'from stock'), ('make_to_order', 'on order')], 'Procurement Method', required=True, readonly=True, states={'draft': [('readonly', False)]},
help="From stock: When needed, the product is taken from the stock or we wait for replenishment.\nOn order: When needed, the product is purchased or produced."),
'price_ subtotal': fields. function(_amount_line, string='Subtotal', digits_compute= dp.get_precision('Account')),
'tax_ id': fields. many2many('account.tax', 'sale_order_tax', 'order_line_id', 'tax_id', 'Taxes', readonly=True, states={'draft': [('readonly', False)]}),
'address_ allotment_ id': fields. many2one('res.partner', 'Allotment Partner',help="A partner to whom the particular product needs to be allotted."),
'product_ uom_ qty': fields. float('Quantity', digits_compute= dp.get_precision('Product UoS'), required=True, readonly=True, states={'draft': [('readonly', False)]}),
'product_ uom': fields. many2one('product.uom', 'Unit of Measure ', required=True, readonly=True, states={'draft': [('readonly', False)]}),
'product_ uos_ qty': fields. float('Quantity (UoS)' ,digits_ compute= dp. get_ precision('Product UoS'), readonly=True, states={'draft': [('readonly', False)]}),
'product_ uos': fields. many2one('product.uom', 'Product UoS'),
'discount': fields. float('Discount (%)', digits_ compute= dp. get_ precision('Discount'), readonly=True, states={'draft': [('readonly', False)]}),
'th_ weight': fields. float('Weight', readonly=True, states={'draft': [('readonly', False)]}),
'state': fields. selection([('cancel', 'Cancelled'),('draft', 'Draft'),('confirmed', 'Confirmed'),('exception', 'Exception'),('done', 'Done')], 'Status', required=True, readonly=True,
help='* The \'Draft\' status is set when the related sales order in draft status. \
\n* The \'Confirmed\' status is set when the related sales order is confirmed. \
\n* The \'Exception\' status is set when the related sales order is set as exception. \
\n* The \'Done\' status is set when the sales order line has been picked. \
\n* The \'Cancelled\' status is set when a user cancel the sales order related.'),
'order_ partner_ id': fields. related('order_id', 'partner_id', type='many2one', relation='res.partner', store=True, string='Customer'),
)





上一篇:RUBY ON RAILS 实用圣经阅读 (3)
下一篇:RecyclerView 未按预期显示数据库内容

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表