Odoo Help

0

BUTTON performing QUERY with POSTGRESQL

By
Lakay lakay
on 9/12/14, 4:59 AM 1,771 views

Hi. im just new in Odoo, please guide me thanks

here is my scenario:

1.) I made a module name 'Shipping information' module here is my .py code

from openerp.osv import osv,fields

class si(osv.osv):

        _name='si'
        _columns ={
        'addr':fields.char('Shipping Address',size=100,required=True),
        'current':fields.boolean('Current'),
}
si()

as you can see it has 2 fields 1.) Shipping Address(char) which the user will input his address, 2.) Current (boolean), each address contains 'current' field  wherein it is with a 'button'.. if the user clicks the button it will toggle the boolean field of a certain record to 'true'.. here is my xml (just a snippet, dont worry it has no defect)

 here is the image to better appreciate : http://postimg.org/image/k0gjwlmcj/

<record model="ir.ui.view" id="view_si_tree">
        <field name="name">si.tree</field>
        <field name="model">si</field>
        <field name="type">tree</field>
        <field name="arch" type="xml">
            <tree string="Shipping Information">
                <field name="addr" />

                <button name="btnSet" string="Set" type="object"/>
            </tree>
        </field>

    </record>

as you can see i use type='object' (or am i wrong? ) because i will call a function which contains the query, im planning to make this function and add to .py file, unfortunately im not able to do that because the function is not finish yet.. can you help me? i want that when the button is pressed it will toggle the boolean variable to 'true' of that certain id.. how can i link this to the button i have made in the xml? 

def set_current_addr(self,cr,uid,ids,name,arg,context=None):

        res={}             #is this necessary? i just saw it in other sites
        cr.execute("""UPDATE si SET current='true' where id=ids;""")

        return res        #is this necessary? i just saw it in other sites

Please guide me so that i can continue my learning in odoo.

also is 'uid' in the function there the create_id i and the 'ids' the id n openerp?

 

Hey ! I hope you got the answer from the below. If yes then please accept it. Thanks.

Emipro Technologies Pvt. Ltd.
on 9/12/14, 10:48 AM
1

Lakay,

It's so easy !

Your method should look like,

def set_current_addr(self,cr,uid,ids,name,arg,context=None):

   self.write(cr,uid,ids,{'current':True},context=context)

   return True

Here by calling write method it is itself applying update query and your current record will be updated. It is adviceable that don't write queries, instead of that use ORM functions. Write is one of the core methods of OpenERP framework. Still if you want to do by query then below is the code.

def set_current_addr(self,cr,uid,ids,name,arg,context=None):

   cr.execute('UPDATE si SET current='true' where id=ids[0]")

   return True

No need of res = {} and return res as per your code.

Hope this helps,

 

 

 

in "ids" argument you will find the current record's id in "Array".

Hiren Vora - CEO at Emipro Technologies Pvt. Ltd. India
on 9/12/14, 5:10 AM

Hi, itried your solution, but it gives me error ProgrammingError: column "ids" does not exist LINE 1: UPDATE si SET current='true' where id=ids[0] here is my code class si(osv.osv): def btnSet(self,cr,uid,ids,context=None): self.write(cr,uid,ids,{'current':True},context=context) cr.execute("UPDATE si SET current='true' where id=ids[0]") return True _name='si' _columns ={ 'addr':fields.char('Shipping Address',size=100,required=True), 'current':fields.boolean('Current'), } si()

Lakay lakay
on 9/14/14, 9:49 PM

Thanks Hiren, I figured it out, so its easy to use ORM functions instead of querying! i learned it!! thanks!! this is my code def btnSet(self,cr,uid,ids,values,context=None): self.write(cr,uid,ids,{'current':'true'}) return True thanks man!!

Lakay lakay
on 9/15/14, 12:01 AM

Thanks Hiren, I figured it out, so its easy to use ORM functions instead of querying! i learned it!! thanks!! this is my code def btnSet(self,cr,uid,ids,values,context=None): self.write(cr,uid,ids,{'current':'true'}) return True thanks man!!

Lakay lakay
on 9/15/14, 12:01 AM
0

<button name="btnSet" string="Set" type="object"/>

This button will call the method name "btnSet" of the class "si". So, you have to defind one method with the name "btnSet" as like below.

def btnSet(self, cr, uid, ids, context=None):

    #Perform your code This is comment.

    self.write(cr,uid,ids, {'current':True}, context=context) #It will write the true value into 'current' field for the record on which button is pressed.

    # you can write here more code as your requirement.

    return True #You can return anything from here.

thanks dude. i learned from Hiren that there is an alternative way (easier way) to modify data in database by using ORM functions such as write.. thanks for giving me solution on how to link the function in .py to button in .xml! thanks man!

Lakay lakay
on 9/15/14, 12:03 AM

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

3 follower(s)

Stats

Asked: 9/12/14, 4:59 AM
Seen: 1771 times
Last updated: 3/16/15, 8:10 AM