问题
When I click on the "Attach PDF" button, the list form opens, but the "Create" button does not work. But when I go to the list form from the main menu, then everything is fine. What is the problem?
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>
**I am superuser rights Odoo 12*
thanks for your answers***
回答1:
The create button should work. all records which have product_id
different from the current product id should be filtered.
The records are filtered using the action domain
, if you do not see records in the tree view, it means that they are hidden.
If you need to fill product_id
with the current product pass default values in the button context:
context="{'default_product_id': active_id}"
To filter records in the tree view and show only records related to the current product, you can use values from context as you did but it is not necessary since you can use active_id
in the action domain:
<field name="domain"> [('product_id', '=', active_id)]</field>
Edit:
When you click on the create button Odoo will try to switch to the form view only if the controller that triggered the request is the current controller.
The current controller is the last controller in the controller stack, i.e. the currently displayed controller in the main window (not in a dialog), and null if there is no controller in the stack.
Try to set the target to
current
, which will let you create, edit, and delete easily.<field name="target">current</field>
Or you can add One2many relation (
inverse_name==product_id
) to add documents to the product template.Example:
Add the One2many relation:
class ProductTemplate(models.Model): _inherit = "product.template" document_ids = fields.One2many('attach.pdf', 'product_id', 'Documents')
Add the following code after
XPath
expression.<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>
来源:https://stackoverflow.com/questions/61947456/why-the-create-button-in-the-main-form-does-not-work