Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
54117 Vistas

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
Descartar

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

Mejor respuesta

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

Avatar
Descartar
Autor

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.

Publicaciones relacionadas Respuestas Vistas Actividad
5
abr 20
11206
1
jun 18
4102
0
may 15
636
0
abr 24
1083
1
oct 23
4716