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

Python Odoo如何通过小部件many2many\u二进制文件为特定字段设置默认值,python,xml,odoo,odoo-8,Python,Xml,Odoo,Odoo 8,我在res_partner和ir_attachement之间有许多字段是这样定义的: class res_partner(osv.osv): _inherit = 'res.partner' _columns = { 'attachments_ids': fields.many2many('ir.attachment', 'res_partner_custom_ir_attachment

我在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",
                                        )
               }
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")
                }
我还修改了ir_附件模型,添加了一个“file_custom_type”:

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",
                                        )
               }
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\u partner\u custom\u ir\u attachment\u rel
仅在xml中字段的最后一次出现时递增(在给定的代码中,它是“类型c”,但如果输入类型c和类型B的字段,则只有类型B会保存在关系表中

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

可视化错误: 当我上传:

刷新页面时:

问题3:域不工作
正如您在上面的问题中所看到的,file_custom_type没有保存,但是我在这个字段上有一个域,但是,第三个字段仍然显示附件,当域状态为时,它应该只显示file_custom_type=“c”。

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

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


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

V8不支持多次在表单视图中使用字段。谢谢,这是我在阅读Kenly的评论后所做的。在发布问题之前,我已经想到了解决方案,但不想使用它,因为它感觉像是不干净的代码。我“我会将您的答案标记为已解决,因为没有其他方法可以解决我的问题。不要忘记使用相同的关系名称,感谢您接受我的答案。我从您的解释中知道,即使我没有提供清晰的代码,您也会理解我所做的事情。还有另一种解决方案使用计算字段和反向方法,但解决方案的复杂性很高。”这不值得。