Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

pool.search() with 'in' operator?

By
Matias Garcia Isaia
on 5/22/13, 7:27 PM 6,093 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()
5

Sandeep

--Sandeep--
6083
| 6 5 8
Delhi, India
--Sandeep--
Python OpenERP Developer http://in.linkedin.com/pub/sandeep-rana/3b/a16/786/
Sandeep
On 5/23/13, 3:47 AM

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

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?

Matias Garcia Isaia
on 5/23/13, 4:18 AM

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

Andreas Maertens
on 5/23/13, 4:28 AM

@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.

Matias Garcia Isaia
on 5/24/13, 4:19 AM

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

Sandeep
on 5/24/13, 4:24 AM

'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.

Hannes Smit
on 8/28/14, 9:21 AM
0

Borni DHIFI

--Borni DHIFI--
5102
| 6 8 9
Tunis, Tunisia
--Borni DHIFI--

Techno Functional Consultant Odoo(OpenERP) 

http://www.linkedin.com/in/borni

Skype : dhifi.borni

Borni DHIFI
On 5/22/13, 7:38 PM

Hi,

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

Thanks.

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])]))

Matias Garcia Isaia
on 5/22/13, 8:26 PM

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

Hannes Smit
on 8/28/14, 9:17 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 5/22/13, 7:27 PM
Seen: 6093 times
Last updated: 7/29/15, 11:52 AM