【问题标题】:Odoo How to have a default value for a specific field via the widget many2many_binaryOdoo 如何通过小部件 many2many_binary 为特定字段设置默认值
【发布时间】:2020-08-19 17:19:16
【问题描述】:

我在 res_partner 和 ir_attachement 之间定义了一个 many2many 字段:

class res_partner(osv.osv):

    _inherit = 'res.partner'


    _columns = {
    'attachments_ids': fields.many2many('ir.attachment',
                                        'res_partner_custom_ir_attachment_rel',
                                        string=u"Custom attachments",
                                        )
               }

我还修改了 ir_attachment 模型,以这种方式添加了“file_custom_type”:

class Attachment(osv.osv):
    _inherit = 'ir.attachment'

    _columns = {
                'file_custom_type': fields.selection([("a", u"Type A"),
                                                      ("b", u"Type B"),
                                                      ("c", u"Type C"),
                                                      ],
                                                      u"Custom file type")
                }

与表单视图顶部的 XXX 数百个附件的下拉列表相比,这将允许我按我的自定义类型重新组合附件,对附件进行排序并获得更清晰的视图。

所以我在 res_partner_form_view 中创建了一个笔记本:

<notebook position="inside">
    <page string="Company attachments" attrs="{'invisible': [('is_company', '=', False)]}">
        <group>
            <field name='attachments_ids'
                   string="Attachment type A"
                   widget='many2many_binary'
                   domain="[('file_custom_type', '=', 'a')]"
                   context="{'default_file_custom_type': 'a'}"/>
            <field name='attachments_ids'
                   string="attachment type B"
                   widget='many2many_binary'
                   domain="[('file_custom_type', '=', 'b')]"
                   context="{'default_file_custom_type': 'b'}"/>
            <field name='attachments_ids'
                   string="attachment type C"
                   widget='many2many_binary'
                   domain="[('file_custom_type', '=', 'c')]"
                   context="{'default_file_custom_type': 'c'}"/> 
            </group>
    </page>
</notebook>

但是有了这个,我遇到了很多问题:

问题 1:file_custom_type 永远不会保存

上下文不起作用:file_custom_type 从未按预期保存,它在数据库中保持空白(通过我服务器上的 psql 请求验证)

问题 2:只有最后一次出现的字段保存在关系表中

当我使用表单视图上传图片时,图片保存在ir_attachment表中,这就是本意。

但是,关系表 res_partner_custom_ir_attachment_rel 仅在 xml 中字段的最后一次出现时递增(在给定的代码中,它是“类型 c”,但如果我将类型 C 和类型 B 的字段相互转换,则只有类型 B 将保存在关系表中。

这导致附件仅显示在最下方的字段(并且仅显示在此字段中输入的附件)

Bug 可视化:

当我上传时:

当我刷新页面时:

问题 3:域不工作

正如你在上面的问题中看到的,file_custom_type 没有保存,但是我在这个字段上有一个域,然而,第三个仍然显示附件,当域状态时它应该只显示 file_custom_type=" c"。

【问题讨论】:

  • v8 不支持在表单视图中多次使用一个字段

标签: python xml odoo odoo-8


【解决方案1】:

我认为最简单的解决方案是在你的类中创建三个字段并确保你定义了域

attachment_type_a = fields.(...., domain=[...])

这里要做的关键是为所有字段使用相同的关系名称和列名称,以确保它们将数据保存在同一个表中,无需创建三个关系表。

当你不关心域时,你可以保留 attachment_ids 来访问所有附件。

【讨论】:

  • 谢谢,这就是我在阅读 Kenly 的评论后所做的。在发布我的问题之前,我已经想到了解决方案,但不想使用它,因为它感觉像是不干净的代码。我会将您的答案标记为已解决,因为没有其他方法可以解决我的问题。
  • 不要忘记使用相同的关系名称,感谢您接受我的回答,我从您的解释中知道,即使我没有提供清晰的代码,您也会明白我所做的,还有另一种解决方案计算字段和逆方法,但解决方案的复杂性不值得。
猜你喜欢
  • 1970-01-01
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
  • 2019-03-23
  • 1970-01-01
相关资源
最近更新 更多