Don't proxy special methods to model in ProxyModel.__getattr__#108
Open
optiz0r wants to merge 2 commits intoclokep:mainfrom
Open
Don't proxy special methods to model in ProxyModel.__getattr__#108optiz0r wants to merge 2 commits intoclokep:mainfrom
optiz0r wants to merge 2 commits intoclokep:mainfrom
Conversation
1707906 to
6f11cdf
Compare
ProxyModel.__getattr__ previously proxied all attribute lookups to the model class, but some third party libraries (e.g. DRF) will make calls which should be handled by the ProxyModel instance rather than the proxied class. For example, deepcopy invokes `__reduce_ex__()` that pickles an instance and needs access to `__getstate__()` which does note exist on a class. Proxying calls to the model is required in some cases, e.g. for access to _meta. This change avoids proxying any special methods (those starting with `__`) to the model. Fixes DRF schema generation for a serializer which contains a field using QuerySetSequence. Adds test cases to verify behaviour of method proxying. Fixes clokep#107
clokep
reviewed
Feb 6, 2025
Comment on lines
495
to
+500
| def __getattr__(self, name): | ||
| return getattr(self._model, name) | ||
| if name == "_meta": | ||
| return getattr(self._model, name) | ||
| if hasattr(super(), "__getattr__"): | ||
| return super().__getattr__(name) | ||
| raise AttributeError(name) |
Owner
There was a problem hiding this comment.
So with this change the only thing being proxied is _meta. Would it make more sense to define only that?
@property
def _meta(self):
return getattr(self._model, "_meta")
Owner
Should there be a test for this and not just the proxy model tests? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ProxyModel.getattr previously proxied all attribute lookups to the model class, but some third party libraries (e.g. DRF) will make calls which should be handled by the ProxyModel instance rather than the proxied class. For example, deepcopy invokes
__reduce_ex__()that pickles an instance and needs access to__getstate__()which does note exist on a class.Proxying calls to the model is required in some cases, e.g. for access to _meta.
This change avoids proxying any special methods (those starting with
__) to the model. Fixes DRF schema generation for a serializer which contains a field using QuerySetSequence.Adds test cases to verify behaviour of method proxying.
Fixes #107