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

Fix bug where descriptor class missed instance=None case #822

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Authors
- Hamish Downer
- Hanyin Zhang
- Hernan Esteves (`sevetseh28 <https://github.com/sevetseh28>`_)
- Hielke Walinga (`hwalinga <https://github.com/hwalinga>`_)
- Jack Cushman (`jcushman <https://github.com/jcushman>`_)
- James Muranga (`jamesmura <https://github.com/jamesmura>`_)
- James Pulec
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changes
Unreleased
----------

- Fixed bug where serializer of djangorestframework crashed if used with ``OrderingFilter`` (gh-821)

3.0.0 (2021-04-16)
------------------

Expand Down
2 changes: 2 additions & 0 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ def __init__(self, model, fields_included):
self.fields_included = fields_included

def __get__(self, instance, owner):
if instance is None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

@hwalinga Why would you want to return the descriptor?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh I see your stackoverflow post. Got it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is so that any metaprogramming done on the class knows what kind of attribute this is. (ie a descriptor class) You can also return None, but this way you allow anyone inspecting the class (programmatically) to know what is in here.

That is also the bug I got in DRF. There was some metaprogramming that failed to continue when it got to this attribute.

return self
values = {f.attname: getattr(instance, f.attname) for f in self.fields_included}
return self.model(**values)

Expand Down