1

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 Pythonic, maybe there is a way to do this using PyCharm only. It would be enough.

Milano
  • 18,048
  • 37
  • 153
  • 353
  • 1
    This seems to be against Python's Zen. As we all know, Python doesn't even have a mandatory scope restriction such as `private`, `protect` or something else. The reason is Python believes in the developers They can do the right things. So with the same Zen, I think you should provide enough document for your users, and notice them that if you don't use it in this way, what will happen etc. – Sraw Nov 21 '18 at 17:53
  • Good use of PR review can be helpful here :) It seems to me it will be overkill to override `__getattribute__` or any kind of hack, for achieving something simple. – ruddra Nov 21 '18 at 18:38

2 Answers2

2

The use of underscores might help here:

class MyModel(Model):

    _my_attr = None

    def get_my_attr_safe(self):
        if self._my_attr is None:
            self._my_attr = somecalculation()
        return self._my_attr

    my_attr = property(get_my_attr_safe)

Taken from this answer

BottleZero
  • 893
  • 6
  • 13
0

I would not recommend overriding __getattr__ or touch anything in Model class. This is the core of Django, if you do something, you might not know where the next bug will pop up. Rather than that, I think its better to use a wrapper around it to get the restrictions there. For example:

class YourModelWrapper(object):
    model_object = None
    restricted_fields = ['some', 'fields']

    def __init__(self, model_object):
        self.model_object =  model_object


    def __getattr__(self, name):
         if name is not in self.restricted_fields:
              return getattr(self.model_object, name)

         raise AttributeError("Use get_{}_safe() method instead".format(name)

# Usage
your_model_wrapper_obj = YourModelWrapper(YourModel.objects.first())
your_model_wrapper_obj.my_attr  # will raise exception
your_model_wrapper_obj.get_my_attr_safe()  # will return the values

FYI it will be a hassle to use this instead of actual model because there is lots of thing missing from this wrapper like queryset support. But there is a good side as well. You have said your model is very complicated, so using a wrapper might help to put some complexities from Model to Wrapper, or use it like a service.

ruddra
  • 50,746
  • 7
  • 78
  • 101