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.

2

How to override a method using inherit?

By
Anabela Damas
on 6/20/13, 3:44 PM 11,224 views

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?

5
Carlos Vasquez
On 6/20/13, 4:34 PM

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

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.

Versão Integral, Anabela Damas
on 6/20/13, 6:49 PM

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

Versão Integral, Anabela Damas
on 6/20/13, 6:50 PM

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)

ClearCorp S.A., Carlos Vasquez
on 6/20/13, 7:57 PM

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

ClearCorp S.A., Carlos Vasquez
on 6/20/13, 7:59 PM

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

Versão Integral, Anabela Damas
on 6/20/13, 8:42 PM

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.

ClearCorp S.A., Carlos Vasquez
on 6/21/13, 6:24 PM

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

Versão Integral, Anabela Damas
on 6/21/13, 7:20 PM

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

Versão Integral, Anabela Damas
on 6/24/13, 6:44 AM

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?

Carlos Llamacho
on 10/10/13, 5:35 PM

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?

fussions
on 2/27/14, 3:59 AM

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.

Carlos Llamacho
on 3/26/14, 1:46 PM
1

patrick

--patrick--
1958
| 5 4 7
Baarn, Netherlands
--patrick--
patrick
On 6/20/13, 4:30 PM

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.

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

Versão Integral, Anabela Damas
on 6/20/13, 5:26 PM

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

patrick
on 6/21/13, 5:23 AM

I tried this:

        import logging
        logging.warning('Something')

and nothing happened. Thanks

Versão Integral, Anabela Damas
on 6/21/13, 6:32 AM

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

patrick
on 6/21/13, 9:59 AM

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

1 follower(s)

Stats

Asked: 6/20/13, 3:44 PM
Seen: 11224 times
Last updated: 8/16/16, 3:33 AM