I'm writing a method to get the attachments of one of my custom objects.
class my_object(osv.osv):
def _get_attachments(self, cr, uid, ids, field_name, arg, context):
res = {}
attach_pool = self.pool.get('ir.attachment')
print "IDs: ", ids
attachs = attach_pool.search(cr, uid, args = [('res_id', 'in', ids)])
print "Attachs: ", attachs
return res
_name = "my.object"
_columns = {
'attachments': fields.function(_get_attachments, method=True, type='char', string='Adjuntos')
}
my_object()
I already know I still have to change the fields.function
type
's attribute, but before that I want to correct an exception I'm getting.
When running this (viewing an instance of this object), I get this output:
IDs: [3]
[2013-05-22 18:13:47,340][db] ERROR:web-services:Uncaught exception
Traceback (most recent call last):
File "/usr/share/pyshared/openerp-server/osv/osv.py", line 122, in wrapper
return f(self, dbname, *args, **kwargs)
File "/usr/share/pyshared/openerp-server/osv/osv.py", line 176, in execute
res = self.execute_cr(cr, uid, obj, method, *args, **kw)
File "/usr/share/pyshared/openerp-server/osv/osv.py", line 167, in execute_cr
return getattr(object, method)(cr, uid, *args, **kw)
File "/usr/share/pyshared/openerp-server/osv/orm.py", line 2947, in read
result = self._read_flat(cr, user, select, fields, context, load)
File "/usr/share/pyshared/openerp-server/osv/orm.py", line 3067, in _read_flat
res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
File "/usr/share/pyshared/openerp-server/osv/fields.py", line 813, in get
res = self._fnct(obj, cr, user, ids, name, self._arg, context)
File "/usr/share/pyshared/openerp-server/addons/my-object/my_object.py", line 49, in _get_attachments
attachs = attach_pool.search(cr, uid, args = [('res_id', 'in', ids)])
File "/usr/share/pyshared/openerp-server/addons/document/document.py", line 185, in search
context=context, count=False)
File "/usr/share/pyshared/openerp-server/addons/base_calendar/base_calendar.py", line 1771, in search
new_args[i] = (arg[0], arg[1], base_calendar_id2real_id(arg[2]))
File "/usr/share/pyshared/openerp-server/addons/base_calendar/base_calendar.py", line 85, in base_calendar_id2real_id
return base_calendar_id and int(base_calendar_id) or base_calendar_id
TypeError: int() argument must be a string or a number, not 'list'
How can I get the 'in' operator named under Expressing a search domain (args)
[1] to work? What's wrong in that particular line?
[ 1 ] doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/methods.html
Just for future readers, here's the final working code:
class my_object(osv.osv):
def _get_attachments(self, cr, uid, ids, field_name, arg, context):
res = {}
attach_pool = self.pool.get('ir.attachment')
print "IDs: ", ids
for i in ids:
attach = attach_pool.search(cr, uid, args = [('res_id', '=', i)])
res[i] = attach
return res
_name = "my.object"
_columns = {
'attachments': fields.function(_get_attachments, method=True, type='one2many', obj='ir.attachment', string='Attachments')
}
my_object()