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.

0

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

By
Rubén Cabrera
on 11/4/15, 6:54 AM 2,210 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(
string="...",
help="...",
comodel_name="other.model",
domain="[('this_same_model_id','=',id)]",
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
1

Axel Mendoza

--Axel Mendoza--
10318
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

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

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

2 follower(s)

Stats

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