Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Artificial Intelligence
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Property Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

how to calculate a fields.function

Subscribe

Get notified when there's activity on this post

This question has been flagged
3 Replies
14924 Views
Avatar
jean44

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 ?

1
Avatar
Discard
Abhishek H Menon

where is your vehicule model table?

Avatar
Abhishek H Menon
Best Answer
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

1
Avatar
Discard
Abhishek H Menon

hope the answer is okay mos

jean44
Author

Thank you brother ! it works.

Avatar
Serpent Consulting Services Pvt. Ltd.
Best Answer

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

0
Avatar
Discard
Avatar
jean44
Author Best Answer

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
Avatar
Discard
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk Slovenščina Español (América Latina) Español Svenska ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now