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.

0

Method not returning any output

By
Emmanuel Roldan
on 8/25/14, 4:48 PM 708 views

I have this method on a button and the problem is that it is not returning anything. Not sure what is it I have to change.

This is my code:

def onchange_product_get_provider(self, cr, uid, ids, product_id, args=None, context=None):
        res = {'value': {'random_seller_info_id':False}}

        for record in self.browse(cr, uid, ids, context=context):
            cr.execute('''select name from product_supplierinfo where product_tmpl_id = (select product_id from itaconloans_e3loan_services) order by RANDOM() limit 1''')
            val = cr.fetchall()
            if val:

                res = {'value': {'random_seller_info_id': val[0]}}
                #res[record.id] = val[0]
            else:
                res = {'value': {'random_seller_info_id': False}}
                #res[record.id] = False
            return res

Edit here is all the code form my python file:

from openerp.osv import osv, fields, orm
from openerp.tools.translate import _
from openerp.tools import config
from datetime import date


#services
class itaconloans_e3loan_services(osv.osv):
    """ Itacon loans application 'E3 Loan Application services' """
    _name = 'itaconloans.e3loan.services'
    _description = 'Loans services'

    def get_product_provider(self, cr, uid, ids, field_name, args=None, context=None):
        res = {}
        for record in self.browse(cr, uid, ids, context=context):
            cr.execute('''select name from product_supplierinfo where product_tmpl_id in (select id from product_template) order by RANDOM() limit 1''')
            val = cr.fetchone()
            if val:
                res[record.id] = val[0]
            else:
                res[record.id] = False
            return res
    _columns = {
        'ServicesID': fields.char('Services ID', size=15),
        'product_id': fields.many2one('product.product', 'name', 'Service Name'),
        'product_is_random': fields.related('product_id', 'is_random', type='boolean', string='Product is Random', readonly=True),

        'AppNum': fields.many2one('itaconloans.e3loan', 'Application Number', select=True),

        'provider': fields.many2one('res.partner', 'Provider Name', select=True),

        'random_seller_info_id': fields.many2one('res.partner', string="Random Supplier", readonly=True),

        'DateReq': fields.date('Request Date'),
        'DateComp': fields.date('Completed Date'),
        'DateToComp': fields.date('Due Date'),
        'comments': fields.text('Comments', translate=True),
    }

    def get_product_get_provider(self, cr, uid, ids, product_id, args=None, context=None):
        res = {'value': {'random_seller_info_id':False}}
        cr.execute('''select name from product_supplierinfo where product_tmpl_id = (select product_id from itaconloans_e3loan_services) order by RANDOM() limit 1''')
        #val = cr.fetchone()
        val = cr.fetchall()
        if val:
            #res = {'value': {'random_seller_info_id': val[0]}}
            res = {'value': {'random_seller_info_id': val[0][0]}}
            #res[record.id] = val[0]
       # else:
         #   res = {'value': {'random_seller_info_id': False}}
            #res[record.id] = False
        return res
itaconloans_e3loan_services()

and from the xml:

        <!-- Loans services Form View -->
        <record model="ir.ui.view" id="view_loans_services_form">
            <field name="name">itaconloans.e3loan.services.form</field>
            <field name="model">itaconloans.e3loan.services</field>
            <field name="arch" type="xml">
                <form string="Loans services" version="7.0">
                    <sheet>
                        <group>
                            <field name="ServicesID"/>
                            <field name="AppNum"/> 

                            <field name="product_id" string="Service Name"/>
                            <field name="product_is_random" />
                            <field name="provider" attrs="{'invisible':[('product_is_random','=',True)]}"/>
                            <field name="DateReq"/>
                            <field name="DateComp"/>
                            <field name="DateToComp"/>
                            <field name="comments"/>
                            <group>
                            <field name="random_seller_info_id" attrs="{'invisible':[('product_is_random','!=',True)]}"/>
                            <button name="get_product_get_provider" string="Random Provider" type="object" attrs="{'invisible':[('product_is_random','!=',True), ('random_seller_info_id','>',0)]}" /> <!-- invisible="True" -->
                            </group>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>

0

Bole

--Bole--
2385
| 5 4 6
Zagreb | Karlovac, Croatia
--Bole--

there is only 10 kind of people the ones that understand binary ... and others

Bole
On 8/26/14, 4:14 AM

here you have some syntax mistakes... 

1. you are using onchange fnction... no need to browse all records (for record in self....) you already got product_id... do yopu need it?
2. in cr.execute you have limit1 ..-> no need for cr.fetchall() .. cr.fetchone() will do the job.. 
3. did you test your sql and made sure it returns desired result?
4. if you do cr.fetchall() then your res = {'value': {'random_seller_info_id': val[0]}} line should be:
-> res = {'value': {'random_seller_info_id': val[0][0]}}

try modifying your onchange method like this:

def onchange_product_get_provider(self, cr, uid,ids,product_id, context=None):
    res = {'value': {'random_seller_info_id':False}}
    cr.execute('''select name from product_supplierinfo where product_tmpl_id = (select product_id from itaconloans_e3loan_services) order by RANDOM() limit 1''')
    val = cr.fetchone()  # expecting a tuple with one record
    #val=cr.fetchall() -> expect a list of tuples containing one tuple with one record!
    if val:
        res = {'value': {'random_seller_info_id': val[0]}}
        #
res = {'value': {'random_seller_info_id': val[0][0]}}   # uncomment if you use cr.fetchall()

    return res

 

Thank you but it is not storing the value on the table. Here is all the code: from openerp.osv import osv, fields, orm from openerp.tools.translate import _ from openerp.tools import config from datetime import date #services class itaconloans_e3loan_services(osv.osv): """ Itacon loans application 'E3 Loan Application services' """ _name = 'itaconloans.e3loan.services' _description = 'Loans services' def get_product_provider(self, cr, uid, ids, field_name, args=None, context=None): res = {} for record in self.browse(cr, uid, ids, context=context): cr.execute('''select name from product_supplierinfo where product_tmpl_id in (select id from product_template) order by RANDOM() limit 1''') val = cr.fetchone() if val: res[record.id] = val[0] else: res[record.id] = False return res _columns = { 'ServicesID': fields.char('Services ID', size=15), 'product_id': fields.many2one('product.product', 'name', 'Service Name'), 'product_is_random': fields.related('product_id', 'is_random', type='boolean', string='Product is Random', readonly=True), 'AppNum': fields.many2one('itaconloans.e3loan', 'Application Number', select=True), 'provider': fields.many2one('res.partner', 'Provider Name', select=True), 'random_seller_info_id': fields.many2one('res.partner', string="Random Supplier", readonly=True), 'DateReq': fields.date('Request Date'), 'DateComp': fields.date('Completed Date'), 'DateToComp': fields.date('Due Date'), 'comments': fields.text('Comments', translate=True), } def get_product_get_provider(self, cr, uid, ids, product_id, args=None, context=None): res = {'value': {'random_seller_info_id':False}} cr.execute('''select name from product_supplierinfo where product_tmpl_id = (select product_id from itaconloans_e3loan_services) order by RANDOM() limit 1''') #val = cr.fetchone() val = cr.fetchall() if val: #res = {'value': {'random_seller_info_id': val[0]}} res = {'value': {'random_seller_info_id': val[0][0]}} #res[record.id] = val[0] # else: # res = {'value': {'random_seller_info_id': False}} #res[record.id] = False return res itaconloans_e3loan_services() and the xml: itaconloans.e3loan.services.form itaconloans.e3loan.services

Emmanuel Roldan
on 9/2/14, 11:21 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

1 follower(s)

Stats

Asked: 8/25/14, 4:48 PM
Seen: 708 times
Last updated: 3/16/15, 8:10 AM