We have a Django
project and I came across this problem multiple times this year.
I will simplify the example:
class MyModel(Model):
my_attr = ....
...
def get_my_attr_safe():
if not self.my_attr:
return somecalculation()
return self.my_attr
I want to force developers to use get_my_attr_safe()
instead of my_attr
.
It's a huge and complicated model.
My idea was to somehow override __getattribute__
and raise Exception if it's called directly but I don't think this would work. Moreover, Django
, of course needs to call sometimes ModelFields
directly so I can't just do it this way.
I want to either raise Exception
or make sure they will get the information that they have to use the method
if possible.
For example I need them to use the method everywhere in templates:
{{ obj.get_my_attr_safe }}
instead of
{{ obj.my_attr }}
The solution doesn't have to be Python
ic, maybe there is a way to do this using PyCharm
only. It would be enough.