It is currently Tue May 22, 2012 10:38 pm

All times are UTC + 2 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: (Solved) Problem with Functional fields with query SQL
PostPosted: Wed Mar 09, 2011 12:43 pm 
Offline

Joined: Tue Feb 15, 2011 2:18 pm
Posts: 26
Hello. I'm new at this and need some help.
I have a method that makes a SQL query to the database, and the result I get is the reference of a product in the specified field. The code is as follows:

class product_product(osv.osv):

def _get_nombre(self, cr, uid, ids, field_name, arg, context):
for i in ids:
sql_req="""
SELECT f.id AS func_id
FROM product_product c
WHERE
(c.default_code = %d)
""" % (i,)
cr.execute(sql_req)
sql_res=cr.dictfetchone()
if sql_res:
res[i] = sql_res['func_id']
else:
res[i] = False
return res

_inherit = 'product.product'
_name = 'product.product'
_columns = {
'campo' : fields.function(_get_nombre, 'campo', type="char", size=64, method=True, store=True),
}

product_product()


I have created products, but the function it returns no value.
Could someone explain what I'm doing wrong.

Thanks. Sorry for my level of english.


Last edited by jdonat on Mon May 16, 2011 8:39 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Problem with Functional fields with query SQL
PostPosted: Wed Mar 09, 2011 1:54 pm 
Offline

Joined: Thu Sep 23, 2010 9:29 pm
Posts: 251
Location: Leeseringen, Germany
Code:
       SELECT f.id AS func_id
       FROM product_product c
       WHERE
           (c.default_code = %d)

First of all: In your SQL there is no definition to f, so f.id would not return anything.
And ids is a list of integer database ids, you compare them to a character field default_code.
So I have to assume you left out something for this to be an example?


Second: instead of using SQL queries which may slow down your system, you should use the optimized browse functions of OpenERP.


Code:
def _get_nombre(self, cr, uid, ids, field_name, arg, context):
    res = {}
    for session in self.browse(cr, uid, ids, context=context):
        if (session.default_code == False):
            res[session.id] = session.product_tmpl_id.name
        else:
            res[session.id] = session.default_code
    return res

_________________
Light travels faster than sound.
That is why some people appear bright until you hear them speak.


Last edited by dieck on Wed Mar 09, 2011 7:27 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Problem with Functional fields with query SQL
PostPosted: Wed Mar 09, 2011 7:21 pm 
Offline

Joined: Tue Feb 15, 2011 2:18 pm
Posts: 26
Thank you very much for answering the post. It's been a great addition for me.

Greetings.


Top
 Profile  
 
 Post subject: Re: Problem with Functional fields with query SQL
PostPosted: Sat May 14, 2011 10:33 am 
Offline

Joined: Sat May 07, 2011 12:04 pm
Posts: 11
Hi,
can someone give me a link or an explanation of the browse functions?

That would be very kind.


Top
 Profile  
 
 Post subject: Re: Problem with Functional fields with query SQL
PostPosted: Sat May 14, 2011 2:18 pm 
Offline

Joined: Thu Sep 23, 2010 9:29 pm
Posts: 251
Location: Leeseringen, Germany
Browse is a kind of "read" returning an iterable object, where you can access the fields as attributes. And it has a specialty: You can traverse over relations.

Code:
sobj = self.pool.get("sale.order")
sales = sobj.search(cr, uid, [('user_id','=',uid)])

for s in sobj.browse(cr, uid, sales):
  print s.partner_invoice_id.partner_id.country_id


Would hangle along from sale.order to res.partner.address to res.partner and accesses the field "country_id" there.

If trying this, you will notice you get a browse object back, because country_id is a relation.
To access the id itself, to be written to another dataset, use country_id.id.

_________________
Light travels faster than sound.
That is why some people appear bright until you hear them speak.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 2 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:

Protected by Anti-Spam ACP