为什么主窗体中的创建按钮不起作用? [英] Why the Create button in the main form does not work?

查看:71
本文介绍了为什么主窗体中的创建按钮不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

当我点击附加 PDF"按钮时,列表表单打开,但创建"按钮不起作用.但是当我从主菜单,然后一切正常.有什么问题?

model.py

from odoo 导入模型、字段、api类 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='属性值',required=True, ondelete='cascade', index=True)file = fields.Binary(string="上传文件")file_name = fields.Char("文件名")

views.xml

</field></记录><record id="attach_file_wizard" model="ir.actions.act_window"><field name="name">附加 PDF</field><field name="type">ir.actions.act_window</field><field name="res_model">attach.pdf</field><field name="view_type">表单</field><field name="view_mode">tree,form</field><字段名称=域">[('product_id', '=', context.get('product_name'))]<field name="view_id" ref="attach_pdf_view_tree"/></记录><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></记录></数据></odoo>

**我是超级用户权限Odoo 12*

感谢您的回答***

解决方案

创建按钮应该可以工作.应过滤所有 product_id 与当前产品 ID 不同的记录.

使用操作domain过滤记录,如果您在树视图中没有看到记录,则表示它们被隐藏了.

如果您需要在按钮上下文中使用当前产品传递默认值填充 product_id:

context="{'default_product_id': active_id}"

要过滤树视图中的记录并仅显示与当前产品相关的记录,您可以像以前一样使用上下文中的值,但没有必要,因为您可以在操作域中使用 active_id:

[('product_id', '=', active_id)]

当您点击创建按钮时,只有当触发请求的控制器是当前控制器时,Odoo 才会尝试切换到表单视图.

<块引用><块引用>

当前控制器是控制器堆栈中的最后一个控制器,即当前显示在主窗口中的控制器(不是在对话框中),如果堆栈中没有控制器,则为 null.

  • 尝试将目标设置为current,这样您就可以轻松创建、编辑和删除.

    current

  • 或者您可以添加 One2many 关系 (inverse_name==product_id) 以将文档添加到产品模板.

    示例:

    添加 One2many 关系:

    class ProductTemplate(models.Model):_inherit = "product.template"document_ids = fields.One2many('attach.pdf', 'product_id', 'Documents')

    XPath 表达式后添加以下代码.

    <page string="文档"><field name="document_ids" widget="one2many_list"><tree editable="底部"><field name="product_attribute_value_id"/><field name="file_name" invisible="1"/><field name="file" widget="binary" filename="file_name"/></field></页面></笔记本>

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***

解决方案

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>
    

这篇关于为什么主窗体中的创建按钮不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆