2
votes

I would like to make it easy to add attachments to the res.partner model in my views. As a result, I've figured out that I can extend the res.partner model easily with a many2many field to ir.attachment.

_columns = {
    'attachments': fields.many2many('ir.attachment', string="Attachments")
}

Now when I add this field to my view I see the list of attachments, but I have 2 problems with this simple many2many widget.

  1. I need too many clicks to get a new file attached. First, it opens the many2many cross table where I could select already uploaded documents. Second, I can open the upload view. The intermediate screen is obviously not needed for me, as this view is used to create new partners, thus the already uploaded files will never be re-used.
  2. I would like the upload view to have its relational fields filled out. These fields are res_model=res.partner, res_id= and partner_id=.

I don't have any ideas on how to get rid of the first, intermediate view problem outlined below.

I've had an idea on how to solve the second problem, to pre-fill out the upload view's data. I wrote this in my view xml

<field name="attachments" 
    context="{'default_res_model': 'res.partner', 'default_res_id': active_id, 'default_partner_id': active_id}"/>

Unfortunately, this does not work, as active_id=0, instead of it being active with the new resource id (this might not even exist?).

Do you have any idea on (1) how to get an immediate upload view and (2) how to get the new record's id into the upload view?

2

2 Answers

1
votes

I've got the solution I was looking for.

Installing the document addon adds a few extra fields, like partner_id to ir.attachment. Thus afterwards, I can add

'attachments': fields.one2many('ir.attachment', 'partner_id',string="Attachments")

to my res.partner extension.

This already allows me to add attachments without the extra view. Unfortunately, one problem remains. As the res_model and res_id fields won't be filled out, the attachment won't be available under the normal res.partner view.

To solve this as well, I've had to extend the ir.attachment model with the following:

from openerp.osv.orm import Model

class document_file(Model):
    _inherit = 'ir.attachment'

    def create(self, cr, uid, vals, context=None):
        if vals.get('partner_id', 0) != 0 and not (vals.get('res_id', False) and vals.get('res_model', False)):
            vals['res_id'] = vals['partner_id']
            vals['res_model'] = 'res.partner'
        return super(document_file, self).create(cr, uid, vals, context)

    def write(self, cr, uid, ids, vals, context=None):
        if vals.get('partner_id', 0) != 0 and not (vals.get('res_id', False) and vals.get('res_model', False)):
            vals['res_id'] = vals['partner_id']
            vals['res_model'] = 'res.partner'
        return super(document_file, self).write(cr, uid, ids, vals, context)

Problem solved! :)

0
votes

I think you should use for (1)

many2many_binary 

widgets for one click attachmnets

<page string="Attachments">
    <field name="attachments" widget="many2many_binary"/>
</page>

Hope this Suggestion will help you