当我单击“附加PDF”按钮时,将打开列表表单,但是“创建”按钮不起作用。但是当我从列表中转到列表主菜单,然后一切都很好。有什么问题吗?
model.py
from odoo import models, fields, api
class AttachPDF(models.Model):
_name = 'attach.pdf'
product_id = fields.Many2one('product.template', string='Product', required=True)
product_attribute_value_id = fields.Many2one('product.attribute.value', string='Attribute Value',
required=True, ondelete='cascade', index=True)
file = fields.Binary(string="Upload file")
file_name = fields.Char("File Name")
views.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="attach_pdf_view_form" model="ir.ui.view">
<field name="name">Attach PDF Form</field>
<field name="model">attach.pdf</field>
<field name="arch" type="xml">
<form>
<group>
<field name="product_id"/>
<field name="product_attribute_value_id"/>
</group>
<group>
<field name="file" widget="binary" filename="file_name" string="Binary"/>
</group>
</form>
</field>
</record>
<record id="attach_pdf_view_tree" model="ir.ui.view">
<field name="name">Attach PDF List</field>
<field name="model">attach.pdf</field>
<field name="arch" type="xml">
<tree>
<field name="product_id"/>
<field name="product_attribute_value_id"/>
<field name="file_name" readonly="1"/>
</tree>
</field>
</record>
<record id="attach_file_wizard" model="ir.actions.act_window">
<field name="name">Attach PDF</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">attach.pdf</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="domain" > [('product_id', '=', context.get('product_name'))]</field>
<field name="view_id" ref="attach_pdf_view_tree"/>
</record>
<record id="view_form_product_attr_pdf" model="ir.ui.view">
<field name="name">attach_pdf_attribute_product_product_template_only_form_view</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view"/>
<field name="arch" type="xml">
<xpath expr="//header/button[@name='121']" position="after">
<button name="%(attach_pdf_attribute.attach_file_wizard)d" context="{'product_name': name}" string="Attach PDF" type="action" class="oe_highlight"/>
</xpath>
</field>
</record>
</data>
</odoo>
**我是超级用户权限Odoo 12 *
感谢您的回答***
创建按钮应该起作用。具有product_id
与当前产品ID不同的所有记录都应过滤。
使用操作domain
对记录进行过滤,如果在树视图中看不到记录,则意味着它们是隐藏的。
如果需要在按钮上下文中用当前产品传递的默认值填充product_id
:
context="{'default_product_id': active_id}"
要在树形视图中过滤记录并仅显示与当前产品相关的记录,您可以像以前一样使用上下文中的值,但这不是必需的,因为可以在操作域中使用active_id
:
<field name="domain"> [('product_id', '=', active_id)]</field>
编辑:
当您单击创建按钮时,Odoo仅在触发请求的控制器是当前控制器的情况下才会尝试切换到表单视图。
当前控制器是控制器堆栈中的最后一个控制器,即当前在主窗口(不在对话框中)显示的控制器,如果堆栈中没有控制器,则为null。
尝试将目标设置为current
,这将使您轻松创建,编辑和删除。
<field name="target">current</field>
或者,您可以添加One2many关系(inverse_name==product_id
)将文档添加到产品模板。
示例:
[添加One2many关系:
class ProductTemplate(models.Model):
_inherit = "product.template"
document_ids = fields.One2many('attach.pdf', 'product_id', 'Documents')
在XPath
表达式后添加以下代码。
<notebook>
<page string="Documents">
<field name="document_ids" widget="one2many_list">
<tree editable="bottom">
<field name="product_attribute_value_id"/>
<field name="file_name" invisible="1"/>
<field name="file" widget="binary" filename="file_name"/>
</tree>
</field>
</page>
</notebook>