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

how to calculate a fields.function

By
jean44
on 9/15/13, 11:13 PM 4,772 views

hello

please ! I need your help I want to add a fields.function 'cost' in my object that returns the sum of two fields from an other object "vehicule". I tried with this code but it seems not working " ProgrammingError: can't adapt type 'dict' "

class location_location(osv.osv):

_name = 'location.location'
def get_total(self, cr, uid, ids, values, arg, context):
x={}
for record in self.browse(cr, uid, ids):
    x[record.id]= record.vehicule_id.tarif_id.prixjournalier + record.vehicule_id.tarif_id.prixhebdomadaire
    return x

_columns = {
    'name': fields.char('ID',size=32,required=True),
    'vehicule_id': fields.many2one('vehicule.vehicule','Véhicule',required=True),
'cost' : fields.function(get_total, method=True, string='Total',type='float'),
}

location_location()

class vehicule_vehicule(osv.osv):

_name = 'vehicule.vehicule'
_columns = {
    'name': fields.char('ID', size=32, required=True, help='L\'identifiant du véhicule '),
'modele_id': fields.many2one('vehicule.modele','Modéle'),
'tarif_id': fields.many2one('vehicule.tarif','Tarif de location'),
    }

vehicule_vehicule()

class vehicule_tarif(osv.osv):

_name = 'vehicule.tarif'
_columns = {
'name': fields.char('ID', size=32, required=True),
    'prixjournalier': fields.integer('Prix journalier'),
    'prixhebdomadaire': fields.integer('Prix hebdomadaire'),
}

vehicule_tarif()

the value calculated will appear in the tree as "cost" please what I can do ?

where is your vehicule model table?

Abhishek H Menon
on 9/18/13, 10:15 AM
1
Abhishek H Menon
On 9/18/13, 10:55 AM
from osv import fields,osv
class vehicule_tarif(osv.osv):
    _name = 'vehicule.tarif'
    _columns = {
                'name': fields.char('ID', size=32, required=True),
                'prixjournalier': fields.integer('Prix journalier'),
                'prixhebdomadaire': fields.integer('Prix hebdomadaire'),
                }
vehicule_tarif()

class vehicule_vehicule(osv.osv):
    _name = 'vehicule.vehicule'
    _columns = {
        'name': fields.char('ID', size=32, required=True, help='L\'identifiant du véhicule '),
#    'modele_id': fields.many2one('vehicule.modele','Modéle'),
       'tarif_id': fields.many2one('vehicule.tarif','Tarif de location'),
        }
vehicule_vehicule()    

class location_location(osv.osv):
    _name = 'location.location'
    def get_total(self, cr, uid, ids, values, arg, context):
        x={}
        print "get_total"
        for record in self.browse(cr, uid, ids):
            x[record.id]= record.vehicule_id.tarif_id.prixjournalier + record.vehicule_id.tarif_id.prixhebdomadaire
        return x

    _columns = {
        'name': fields.char('Name',size=32,required=True),
        'vehicule_id': fields.many2one('vehicule.vehicule','Véhicule',required=True),
        'cost' : fields.function(get_total, method=True, string='Total',type='float'),
        }
location_location()  

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

and xml code for the above models

<openerp>
    <data>

    <!-- Test model View -->
    <menuitem id="menu_demo_sample" name="Demo Menu"
             sequence="3"/>
    <menuitem id="menu_demo_sub1" name="Locations" parent= "menu_demo_sample" sequence="1"/>

    <!-- Name : tarif : Tree View -->

    <record id="view_tarif_tree" model="ir.ui.view">
            <field name="name">vehicule.tarif</field>
            <field name="model">vehicule.tarif</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="Tarif">
                    <field name="name"/>

                </tree>
            </field>        
        </record>




        <!-- Name : tarif : Form View -->

        <record id="view_tarif_form" model="ir.ui.view">
            <field name="name">vehicule.tarif</field>
            <field name="model">vehicule.tarif</field>
            <field name="arch" type="xml">
                <form string="Tarif" version="7.0">
                    <group>         
                        <field name="name"/>
                        <field name="prixjournalier"/>
                        <field name="prixhebdomadaire"/>
                    </group>
                </form>
            </field>        
        </record>

        <!-- Name : vehicule : Tree View -->

    <record id="view_vehicule_tree" model="ir.ui.view">
            <field name="name">vehicule.vehicule</field>
            <field name="model">vehicule.vehicule</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="Vehicule">
                    <field name="name"/>


                </tree>
            </field>        
        </record>




        <!-- Name : vehicule : Form View -->

        <record id="view_vehicule_form" model="ir.ui.view">
            <field name="name">vehicule.vehicule</field>
            <field name="model">vehicule.vehicule</field>
            <field name="arch" type="xml">
                <form string="Vehicule" version="7.0">
                    <group>         
                        <field name="name"/>
                        <field name="tarif_id"/>
                    </group>
                </form>
            </field>        
        </record>

    <!-- Name : test : Tree View -->

        <record id="view_demo_tree" model="ir.ui.view">
            <field name="name">location.location</field>
            <field name="model">location.location</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="Location">
                    <field name="name"/>

                </tree>
            </field>        
        </record>




        <!-- Name : test : Form View -->

        <record id="view_demo_form" model="ir.ui.view">
            <field name="name">location.location</field>
            <field name="model">location.location</field>
            <field name="type">form</field>

            <field name="arch" type="xml">
                <form string="Location" version="7.0">
                    <group>             
                        <field name="name"/>
                        <field name="vehicule_id"/>
                        <field name="cost"/>
                    </group>    
                </form>
            </field>        
        </record>

        <!--Name : test Action : Action Menu  -->

        <record id="action_demo_menu" model="ir.actions.act_window">
            <field name="name">Demo Menu</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">location.location</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="view_id" ref="view_demo_tree"/>
        </record>


       <menuitem name="Sample Result" action="action_demo_menu" id="menu_demo_sub2" parent="menu_demo_sub1" sequence="0"/>
    </data>
</openerp>

after entering a value save the record thereby you can see the total price. thank you

hope the answer is okay mos

Abhishek H Menon
on 9/18/13, 10:59 AM

Thank you brother ! it works.

jean44
on 9/19/13, 6:08 AM
0
jean44
On 9/16/13, 6:10 PM

Thank you for your answer, the return is already out of the loop, I just made a mistake when I was typing the code here. Always the same problem :(

Please can you guive a simple exemple of the fields.function that illustrating how to use them. Thank you.

0

Serpent Consulting Services Pvt. Ltd.

--Serpent Consulting Services Pvt. Ltd.--
4341
| 6 6 8
Gandhinagar, India
--Serpent Consulting Services Pvt. Ltd.--

Serpent Consulting Services Pvt. Ltd. Your Odoo/OpenERP Solution, just an email away!

Serpent Consulting Services Pvt. Ltd.
On 9/16/13, 3:41 PM

If I am not wrong, you have kept the return x inside for loop. Get it out of the loop and see. Thanks.

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: 9/15/13, 11:13 PM
Seen: 4772 times
Last updated: 3/16/15, 8:10 AM