Search code examples
pythonxmlodooodoo-8

Odoo How to have a default value for a specific field via the widget many2many_binary


I've a many2many field between res_partner and ir_attachement defined that way :

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

I also modified the ir_attachment model by adding a 'file_custom_type' that way :

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")
                }

This would allow me to regroup attachment by my custom type, to sort my attachment and have a clearer view than with just the dropdown list of XXX hundreds attachment at the top of my form view.

So I create a notebook in my 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>

But with this, I encounter multiple problems :

PROBLEM 1 : file_custom_type are never saved

Context does not works : the file_custom_type is never saved as intended, it stay's blank in the database (verified with psql request on my server)

PROBLEM 2 : Only the last occurence of the field saves in the relation table

When I use the form view to upload a picture, the picture is saved in the ir_attachment table, which is what is intended.

However, the relation table res_partner_custom_ir_attachment_rel is incremented only for the last occurence of the field in the xml (in the code given, it's "type c" but if I intervert the fields of type C and type B, only the type B would be saved in the relation table.

This result in the attachments beeing displayed only for the down-most field (and only the attachments that where inputed in this field)

Bug visualised :

When I upload : enter image description here

When I refresh the page : enter image description here

PROBLEM 3 : Domain is not working

As you could see in the above problems, file_custom_type is not saved, yet I've got a domain on this field, and yet, the 3rd is still displaying the attachment, when the domains state's it should only display file_custom_type="c".


Solution

  • I think the easiest solution is to create three fields in your class and make sure you define domain

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

    The key thing to do here is to use the same relation name and column names for all of your fields to make sure they save the data in the same table no need create three relation tables.

    An you can keep attachment_ids to access all attachment when don't care about domain.