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
    • Approvals
    • 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
    • Estate 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

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
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 override a method using inherit?

Subscribe

Get notified when there's activity on this post

This question has been flagged
calendarinheritanceinheritoverride
2 Replies
34325 Views
Avatar
Anabela Damas

Hi,

I'm trying to override a method in the class calendar_event, I see in here that is needed to re-define columns/fields but my function don have any field defined in the columns...

So what I've tried was this :

class calendar_event(osv.osv):

    _name = 'calendar.event'
    _inherit = "calendar.event"


    def create_attendees(self, cr, uid, ids, context):
        att_obj = self.pool.get('calendar.attendee')

        print 'JUST Print something to see if it uses this function '

        user_obj = self.pool.get('res.users')

        current_user = user_obj.browse(cr, uid, uid, context=context)
        for event in self.browse(cr, uid, ids, context):
            attendees = {}
            for att in event.attendee_ids:
                attendees[att.partner_id.id] = True
            new_attendees = []
            mail_to = ""
            for partner in event.partner_ids:
                if partner.id in attendees:
                    continue
                att_id = self.pool.get('calendar.attendee').create(cr, uid, {
                    'partner_id': partner.id,
                    'user_id': partner.user_ids and partner.user_ids[0].id or False,
                    'ref': self._name+','+str(event.id),
                    'email': partner.email
                }, context=context)
                if partner.email:
                    mail_to = mail_to + " " + partner.email
                self.write(cr, uid, [event.id], {
                    'attendee_ids': [(4, att_id)]
                }, context=context)
                new_attendees.append(att_id)

            if mail_to and current_user.email:
                att_obj._send_mail(cr, uid, new_attendees, mail_to,
                    email_from = current_user.email, context=context)
        return True


calendar_event()

How can I do this?

2
Avatar
Discard
Avatar
Carlos Vasquez
Best Answer

Hi Anabela,

You only need to redefine columns when you are overwritting a method for a function field. In this case you are not. This method is not used to calculate a field, but to do some other functionality.

The way you are doing this inherit is right. Just a couple of recommendations (assuming you are on v7):

  • Use python standards for class name (camelCase). For your class it would be calendarEvent.
  • Use the new orm.Model object instead of osv.osv. For this you have to import osv.orm first.
  • You don't need anymore the explicit call at the end of the class.
  • For maintainability reasons, always try to implement you inherited methods calling the original one, instead of just copy-pasting it and changing it in your code. This can be done with the super method. In your case it would be:

    super(calendarEvent, self).create_attendees(cr, uid, ids, context)

Regards

6
Avatar
Discard
Anabela Damas
Author

Hi Carlos,

I'm a newbie in python, I do stuff like I see in other modules =)

Thanks for the recommendations ;)

Some doubts...

In the second dot I did like this :

from openerp.osv import fields, orm

class calendar_event(orm.Model):

#_name = 'calendar.event'
_inherit = 'calendar.event'

And nothing happened

As I am newbie in python the dot four is very hard because I never know where to put the code, and copy and paste all code is much easier =) Because I want to change some lines in the middle of the function.

Anabela Damas
Author

So I try everything and the openerp is still using the original def create_attendees(self, cr, uid, ids, context) .... I don't know what I'm doing wrong =(

Thanks

Carlos Vasquez

Have you created the __init__.py for your module? In this __init__.py you need to have a simple line with: "import file". (change file with the name of your py file without the .py)

Carlos Vasquez

Also, when you make changes to the __openerp__.py and the __init__.py, it is usually necessary to update your module in your database.

Anabela Damas
Author

Unfortunately yes, have all of that ... I really don't know what is wrong, I was doing this inherit in a module that does other stuff.

And for tests I created a new single module just with this (mymodule.py), a __init__.py and the __openerp__.py files. This function for some reason that I don't understand doesn't override the original function.... what is weird is that I've made this to other functions and it's working.... =(

Thanks

Carlos Vasquez

One possible explanation is that there is another module installed that is also inheriting this method. If that other module takes precedence, and doesn't do a super call (as I explained in my answer) it will only run that particular module's code. If you are doing this for other methods with a positive result, than it can be either a typo somewhere, or something a little more complicated. It would take a closer look to your code, server and database to find the problem.

Anabela Damas
Author

Thanks for all =) I've done a workaround =)

Anabela Damas
Author

The workaround has a bug, so I tried again to find the problem I've made a grep to find where this function appears and the only places is my code and the original...

Carlos Llamacho

Hi, i have always used the osv.osv model to do my classes but now i read that the new orm.Model is prefered. There is documentation regarding the differences between them?

fussions

Carlos, can you tell me where I can read about using orm.Model object instead osv.osv. Is it mentioned in OpenERP documentation or one can only derive it from the OpenErp source code?

Carlos Llamacho

I haven't found any official documentation regarding orm.Model as better to use than osv.osv. I have however asked the same question in an Openerp Google group and they recommend to start using orm.Model instead of osv.osv as the later is going to be replaced. Only maintain osv.osv for compatibility issues with former versions.

Avatar
patrick
Best Answer

No need to define _name, as your module will inherit the class, with the name.

After making your module, you have to install it. The module (with other files and folders) needs to be in /addons directory (on Ubuntu: /usr/lib/pymodules/python2.7/openerp/addons). Now you go to Settings -> Update Module List, than Settings -> Installed modules. Remove the filter 'installed', and search for your module. Click on it, and click on the button 'Install'.

If you want to see some output, don't use print, but use logging.warning('your text here'). You have to import logging first, before usage.

1
Avatar
Discard
Anabela Damas
Author

from openerp.osv import logging

2013-06-20 19:25:32,114 15008 CRITICAL teste20jun openerp.modules.module: Couldn't load module gestao_ideias 2013-06-20 19:25:32,114 15008 CRITICAL teste20jun openerp.modules.module: cannot import name logging 2013-06-20 19:25:32,115 15008 ERROR teste20jun openerp.netsvc: cannot import name logging

logging.warning('your text here')

I try this way but didn't work...

patrick

I use the text 'import logging', and it is working. It is a default python thing you import, not something from openERP.

Anabela Damas
Author

I tried this:

        import logging
        logging.warning('Something')

and nothing happened. Thanks

patrick

The result of the warning should be in your logging file (on Ubuntu: /vaar/log/openerp/openerp-server.log).

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
Related Posts Replies Views Activity
how to override an inherited method? Solved
inheritance inherit method override
Avatar
Avatar
Avatar
Avatar
5
Jul 18
24324
How to extend an existing class and include social newtork features in the sub class
inheritance inherit
Avatar
Avatar
3
Jul 18
4638
How to inherit a selection field without old values ? Solved
fields inheritance override
Avatar
Avatar
1
Oct 22
7566
Overwrite a model / a method in I10n_de/models/datev
inherit overwrite override
Avatar
0
Jul 22
2862
Inheritance in discuss module (mail)
inheritance inherit discuss
Avatar
0
Jan 22
3383
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 ภาษาไทย 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