How to force/warn way devs treat class/django model
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
1
This seems to be against Python's Zen. As we all know, Python doesn't even have a mandatory scope restriction such asprivate,protector 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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
python django pycharm
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 asprivate,protector 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
add a comment |
1
This seems to be against Python's Zen. As we all know, Python doesn't even have a mandatory scope restriction such asprivate,protector 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
add a comment |
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
add a comment |
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.
add a comment |
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
add a comment |
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
add a comment |
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
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
answered 2 days ago
Bott0610
483211
483211
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited 2 days ago
answered 2 days ago
ruddra
8,32832546
8,32832546
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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,protector 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