Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odoo how overwrite default function new API

I am trying to overwrite class Message method def _get_default_author(self) by this example: by this example

The original model:

class Message(models.Model):
    _name = 'mail.message'
    _inherit = ['ir.needaction_mixin']

     @api.model
     def _get_default_author(self):
         return self.env.user.partner_id

     author_id = fields.Many2one(
             'res.partner', 'Author', select=1,
             ondelete='set null', default=_get_default_author,
             help="Author of the message. If not set, email_from may hold an email address that did not match any partner.")

The inherited model:

class MyClass(models.Model):
    _name = 'my.class'
    _inherit = ['mail.thread', 'ir.needaction_mixin']

    @api.model
    def _get_default_author(self):
        new = self.env['res.users'].search([('id', '=', '1')])
        return new.partner_id

    author_id = fields.Many2one(default=_get_default_author)

But I got an error:

File "/opt/odoo/openerp/models.py", line 4113, in create
    vals = self._add_missing_default_values(vals)
  File "/opt/odoo/openerp/api.py", line 236, in wrapper
    return new_api(self, *args, **kwargs)
  File "/opt/odoo/openerp/api.py", line 478, in new_api
    result = method(self._model, cr, uid, *args, **old_kwargs)
  File "/opt/odoo/openerp/models.py", line 1776, in _add_missing_default_values
    defaults = self.default_get(cr, uid, list(missing_defaults), context)
  File "/opt/odoo/openerp/api.py", line 238, in wrapper
    return old_api(self, *args, **kwargs)
  File "/opt/odoo/openerp/api.py", line 342, in old_api
    result = method(recs, *args, **kwargs)
  File "/opt/odoo/openerp/models.py", line 1336, in default_get
    defaults = self._convert_to_cache(defaults, validate=False)
  File "/opt/odoo/openerp/models.py", line 5454, in _convert_to_cache
    for name, value in values.iteritems()
  File "/opt/odoo/openerp/models.py", line 5455, in <dictcomp>
    if name in fields
  File "/opt/odoo/openerp/fields.py", line 1655, in convert_to_cache
    raise ValueError("Wrong value for %s: %r" % (self, value))
ValueError: Wrong value for my.class.author_id: res.partner(3,)

What I am doing wrong? Maybe there are another way to overwrite default function?

UPDATED: Just for testing I made the same changes for core class Message(models.Model). And everything was alright. So as I understand the problem is with my code. Maybe I overwrite it wrong..

like image 476
fueggit Avatar asked Feb 14 '26 20:02

fueggit


1 Answers

You're not overriding nor extending mail.message but creating your own model my.class. For extending or overriding an Odoo model you just have to use _inherit class attribute:

class MyClass(models.Model):
    _inherit = 'mail.message'
    _name = 'mail.message'  # optional!

    @api.model
    def _get_default_author(self):
        new = self.env['res.users'].search([('id', '=', '1')])
        return new.partner_id

    author_id = fields.Many2one(default=_get_default_author)

Now to your error. You're creating your own model my.class with a field author_id. This field is a many2one relational field, which always needs a comodel to get a relation. By thinking "i will override another models default function" you of course didn't fill the comodel_name parameter, because on overriding, that's not needed.

But you aren't overriding. So Odoo is filling up comodel_name with object(). Your default method is returning a res.partner record, but Odoo is expecting a object record.

So the error is just an aftereffect of the wrong attempt of inheriting/overriding of an exisiting Odoo model.

like image 184
CZoellner Avatar answered Feb 19 '26 04:02

CZoellner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!