Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
2 Antwoorden
8091 Weergaven

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?

 

Avatar
Annuleer

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

Beste antwoord

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,

 

 

 

Avatar
Annuleer

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

Auteur

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

Auteur

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!!

Auteur

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!!

Beste antwoord

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

Avatar
Annuleer
Auteur

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!

Gerelateerde posts Antwoorden Weergaven Activiteit
2
mrt. 15
4228
1
mrt. 15
3713
0
mrt. 15
3347
2
feb. 17
7253
2
apr. 15
14656