Odoo如何通过小部件many2many_binary为特定字段设置默认值 可视化的错误:

问题描述

我在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",)
               }

我还通过添加'file_custom_type'来修改ir_attachment模型:

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。

这导致仅在最下方的字段(以及仅在此字段中输入的附件)显示附件蜂鸣声

可视化的错误:

当我上传时:

enter image description here

刷新页面时:

enter image description here

问题3:域不起作用

如您在上面的问题中看到的,file_custom_type未保存,但我在此字段上有一个域,但是,第3个仍在显示附件,当域状态为时,它仅应显示file_custom_type =“ c”。

解决方法

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

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

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...