This question has been flagged
2 Replies
17548 Views

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()
Avatar
Discard
Best Answer

Hi matias as fucntion field method return value with specific value , and your type is also wrong , you did not do well defined function method your function should be :

  def _get_attachments(self, cr, uid, ids, field_name, arg, context):
    res = {}
    attach_pool = self.pool.get('ir.attachment')
    print "IDs: ", ids
    for id in ids:
        attach = attach_pool.search(cr, uid,[('res_id', '=', id)])
        res[id]=attach
    print "Attachs: ", attachs
    return res

Thanks
Sandeep

Avatar
Discard
Author

Thank you so much. It was really useful. The only thing to point out - id is a Python keyword, so you can't use it as a variable. I've chosen just i. Do you know anything about the in operator? Does it really exist/work?

you can use "id" as var, that is no problem.

Author

@14348 you're right. It was giving me an error about using the id function, but it must have been a mistake - probably forgot to name it id in the for.

man actually there is no problem in id just you should remember that another variable should not be same , you all just forget id , it is just identifier and we can use it as we want

'id()' is a builtin function of Python. If you use a local variable called 'id', you can not use that function anymore. The same would happen if you create a local variable called 'dir'. Then the builtin function 'dir()' becomes unusable.

Best Answer

Hi,

 attachs = attach_pool.search(cr, uid, [('res_id', 'in', [ids])])

Thanks.

Avatar
Discard
Author

It doesn't work. I'm getting the same error, but with that line updated (File "/usr/share/pyshared/openerp-server/addons/my-object/my_object.py", line 49, in _get_attachments attachs = attach_pool.search(cr, uid, [('res_id', 'in', [ids])]))

Borni means: attachs = attach_pool.search(cr, uid, [('res_id', 'in', ids)])