How to force/warn way devs treat class/django model











up vote
1
down vote

favorite
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.










share|improve this question




















  • 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
    2 days ago










  • 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
    2 days ago

















up vote
1
down vote

favorite
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.










share|improve this question




















  • 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
    2 days ago










  • 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
    2 days ago















up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






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.










share|improve this question















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.







python django pycharm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked 2 days ago









Milano Slesarik

4,1321139110




4,1321139110








  • 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
    2 days ago










  • 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
    2 days ago
















  • 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
    2 days ago










  • 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
    2 days ago










1




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
2 days ago




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
2 days ago












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
2 days ago






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
2 days ago














2 Answers
2






active

oldest

votes

















up vote
2
down vote













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






share|improve this answer




























    up vote
    0
    down vote













    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.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417552%2fhow-to-force-warn-way-devs-treat-class-django-model%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      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






      share|improve this answer

























        up vote
        2
        down vote













        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






        share|improve this answer























          up vote
          2
          down vote










          up vote
          2
          down vote









          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






          share|improve this answer












          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          Bott0610

          483211




          483211
























              up vote
              0
              down vote













              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.






              share|improve this answer



























                up vote
                0
                down vote













                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.






                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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.






                  share|improve this answer














                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 days ago

























                  answered 2 days ago









                  ruddra

                  8,32832546




                  8,32832546






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417552%2fhow-to-force-warn-way-devs-treat-class-django-model%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Catalogne

                      Violoncelliste

                      Héron pourpré