Reputation: 2212
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.
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?
Upvotes: 2
Views: 9199
Reputation: 2212
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! :)
Upvotes: 1
Reputation: 1055
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
Upvotes: 0