Odoo 11:在many2many字段上的域不起作用

时间:2018-10-25 09:37:17

标签: odoo-11

<record model="ir.actions.act_window" id="action_purchase_orders">
    <field name="name">Purchase Orders</field>
    <field name="type">ir.actions.act_window</field>
    <field name="res_model">purchase.order</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="domain">[('id', 'in', purchase_order_ids.ids)]</field> <------- This is not working.
</record>


<record id="purchase.form" model="ir.ui.view">
    <field name="name">purchase.form</field>
    <field name="model">purchase.form</field>
    <field name="arch" type="xml">
        <form string="Purchase Form">                
            <sheet>                    
                <div class="oe_button_box" name="button_box">
                    <button type="action" name="%(action_purchase_orders)d" class="oe_stat_button" icon="fa-list-alt"
                            attrs="{'invisible':[('purchase_order_count', '=', 0)]}">
                        <field name="purchase_order_count" invisible="1"/>
                        <field name="purchase_order_ids" string="Purchase Orders" widget="statinfo" help="Purchase orders"/>
                    </button>
...

class PurchaseForm(models.Model):
    _name = 'purchase.form'

    @api.depends('purchase_order_ids')
    def _purchase_order_count(self):
       for pp in self:
           pp.purchase_order_count = len(pp.purchase_order_ids)

    purchase_order_ids = fields.Many2many('purchase.order', 'purchase_form_purchase_order_rel', 'purchase_form_id', 'purchase_order_id', 'Purchase Orders', states=READONLY_STATES)
    purchase_order_count = fields.Integer(compute='_purchase_order_count', string='# of Purchase Orders')

Error:

Uncaught Error: NameError: name 'purchase_order_ids' is not defined
http://localhost:8069/web/content/801-4161526/web.assets_backend.js:144
Traceback:
Error: NameError: name 'purchase_order_ids' is not defined
    at PY_ensurepy (http://localhost:8069/web/content/801-4161526/web.assets_backend.js:144:65)
    at Object.py.evaluate (http://localhost:8069/web/content/801-4161526/web.assets_backend.js:259:8)
    at Object.py.evaluate (http://localhost:8069/web/content/801-4161526/web.assets_backend.js:267:99)
    at Object.py.evaluate (http://localhost:8069/web/content/801-4161526/web.assets_backend.js:268:194)
    at Object.py.eval (http://localhost:8069/web/content/801-4161526/web.assets_backend.js:272:284)
    at http://localhost:8069/web/content/801-4161526/web.assets_backend.js:377:110
    at iterator (http://localhost:8069/web/content/800-e6de2a4/web.assets_common.js:14:183)
    at Function.<anonymous> (http://localhost:8069/web/content/800-e6de2a4/web.assets_common.js:17:8)
    at _.(anonymous function) [as reduce] (http://localhost:8069/web/content/800-e6de2a4/web.assets_common.js:69:526)
    at eval_contexts (http://localhost:8069/web/content/801-4161526/web.assets_backend.js:375:136)

2 个答案:

答案 0 :(得分:1)

您不能在%let last_saturday = intnx ('week.7', today()-1, 0); %let last_saturday = %sysfunc(intnx (week.7, %sysfunc(today())-1, 0), yymmdd10.); %put NOTE: &last_saturday; %let last_saturday_sq = %sysfunc(quote(&last_saturday,%str(%'))); %put NOTE: &last_saturday_sq; proc sql; … connection to … (cast &last_saturday_sq as DATE) as asof … 视图操作中执行此操作,也不能使用域元组右侧的字段,因为将没有上下文来提供该值。为了完成它,您将需要将按钮更改为object类型,并使用关联的python方法返回具有所评估的所需域值的action dict,以便该域仅包含其右侧元组的值。点赞(查看按钮的新类型和名称):

action_purchase_orders

在您的<button type="object" name="action_purchase_orders" class="oe_stat_button" icon="fa-list-alt" attrs="{'invisible':[('purchase_order_count', '=', 0)]}"> <field name="purchase_order_count" invisible="1"/> <field name="purchase_order_ids" string="Purchase Orders" widget="statinfo" help="Purchase orders"/> </button> 模型中,定义方法purchase.form,例如:

action_purchase_orders

答案 1 :(得分:0)

添加与采购订单的关系

class PurchaseOrder(models.Model):
_inherit = 'purchase.order'

   purchase_form_ids = fields.Many2many('purchase.form', 'purchase_form_purchase_order_rel', 'purchase_order_id', 'purchase_form_id', 'Purchase Orders')

并将操作视图更改为

<record model="ir.actions.act_window" id="action_purchase_orders">
    <field name="name">Purchase Orders</field>
    <field name="type">ir.actions.act_window</field>
    <field name="res_model">purchase.order</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="domain">[('id', 'in', purchase_form_ids.ids)]</field> 
</record>