Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't proxy special methods to model in ProxyModel.__getattr__ #108

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

optiz0r
Copy link
Contributor

@optiz0r optiz0r commented Oct 19, 2024

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

@optiz0r optiz0r requested a review from clokep as a code owner October 19, 2024 12:02
@optiz0r optiz0r force-pushed the fix107_drf branch 8 times, most recently from 1707906 to 6f11cdf Compare October 19, 2024 16:29
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
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

@clokep
Copy link
Owner

clokep commented Feb 6, 2025

Fixes DRF schema generation for a serializer which contains a field using QuerySetSequence.

Should there be a test for this and not just the proxy model tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DRF breakage
2 participants