Odoo Help


"NameError: name 'self' is not defined" when trying to call method from own model

Rubén Cabrera
on 11/4/15, 6:54 AM 4,445 views

I'm using context from the model for 2 Many2one fields. Both link to the same model, but context should be different depending on a search result.

These are the relevant parts of the code:

class SomeModel(models.Model):
_inherits = ...
_name = ...
def _search_function(self):
# some logic here

return self.env['related.model'].search_read(...)[0]['id']
# ...
first_relational_field = fields.Many2one(
context="{'default_related_model_id': "+ str(self._search_function()) +"}",

But the next error raises when upgrading the module:


context="{'default_price_version_id': "+ str(self._search_function()) +"}"

NameError: name 'self' is not defined

Can methods be defined inside a model and then called to generate parameters in the fields?

Why is self not available for the model?


try to make a print to your self._search_function() and let me know please

Drees Far
on 11/4/15, 8:34 AM
Axel Mendoza
On 11/4/15, 2:33 PM

You cannot use self when defining class properties. Self only exist when you are running code inside a class method. 

That's right, my mistake, thank you. I ended trying self because I can't manage to call a method to return a value I want to insert into the context string. Calling just _search_function() raises TypeError: wrapper() takes at least 1 argument (0 given) The only argument _search_function() takes is self. Using a callable as we do when passing a compute function doesn't work either, raises no errors on update, but when the context is passed to the view I get this: Error: NameError: name '_get_default_version' is not defined Thanks a lot for you input.

Rubén Cabrera
on 11/6/15, 5:31 AM

About This Community

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


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)


Asked: 11/4/15, 6:54 AM
Seen: 4445 times
Last updated: 4/28/16, 9:33 AM