This question has been flagged
2 Replies
52748 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?



 

Avatar
Discard

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

Best Answer

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

Avatar
Discard
Author

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.