Odoo如何通过小部件many2many\u二进制文件为特定字段设置默认值

2024-05-23 18:48:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我在res_partner和ir_attachement之间有许多字段是这样定义的:

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_附件模型,添加了一个“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_视图中创建了一个笔记本:

<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:文件\自定义\类型从未保存

上下文不起作用:文件\自定义\类型从未按预期保存,它在数据库中为空(在我的服务器上通过psql请求验证)

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

当我使用表单视图上传图片时,图片保存在ir_附件表中,这正是我想要的

但是,关系表res_partner_custom_ir_attachment_rel仅在xml中最后一次出现字段时才递增(在给定的代码中,它是“c类型”,但如果输入c类型和B类型的字段,则只有B类型会保存在关系表中

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

可视化错误:

当我上传:enter image description here

刷新页面时:enter image description here

问题3:域不工作

正如您在上述问题中所看到的,文件\自定义\类型未保存,但我在该字段中有一个域,而第三个域仍在显示附件,当域状态为时,它应仅显示文件\自定义\类型=“c”


Tags: 视图ids类型partner附件attachmentstringir
1条回答
网友
1楼 · 发布于 2024-05-23 18:48:38

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

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

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

您可以保留附件ID,以便在不关心域的情况下访问所有附件

相关问题 更多 >