-
Notifications
You must be signed in to change notification settings - Fork 300
Allow users to overwrite get_serializer_class
while using related urls
#860
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
Changes from all commits
ff191c8
9f69484
a2b5704
5c4cb88
0366ceb
7e577d9
67d79ff
41785b8
e27b7a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,5 @@ Sergey Kolomenkin <https://kolomenkin.com> | |
Stas S. <[email protected]> | ||
Tim Selman <[email protected]> | ||
Tom Glowka <[email protected]> | ||
Ulrich Schuster <[email protected]> | ||
Yaniv Peer <[email protected]> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,10 +144,15 @@ def retrieve_related(self, request, *args, **kwargs): | |
if isinstance(instance, Iterable): | ||
serializer_kwargs['many'] = True | ||
|
||
serializer = self.get_serializer(instance, **serializer_kwargs) | ||
serializer = self.get_related_serializer(instance, **serializer_kwargs) | ||
return Response(serializer.data) | ||
|
||
def get_serializer_class(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removing this exposes the upstream DRF There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My whole point in making the change here is to do exactly this: expose the upstream method, so that it can be overwritten. The current version of DJA does break the path to the upstream method. There might be cases where DJA users rely on this bug in some way. The change here would break their code. I'm just a first-time user of DJA without any idea about how it is being used in other projects, what the effects might be, and who might be affected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forgive me for being dense but how is get_serializer_class() in DJA not able to be overridden by you? You've completely removed the function from DJA; not just added This extension library routinely overrides the upstream DRF because it is extending it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do Full understand that you don't want to run the risk of breaking someone's application. And given that I am fairly new to DJA, I might not fully understand its inner workings, either. I came across the issue the present PR is trying to solve, when I had to overwrite My first attempt to fix the issue was to have To make this work, I need to do some other adjustments along the call chain. In particular, I hope this helps to provide some understanding of what I am trying to accomplish. I was very much focussed on the functionality of getting related resources; because of my limited understanding of DJA, I am not able to tell if other parts of its functionality are affected by the changes introduced in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me try to shed some light on this... If someone overwrote Therefore I don't see that this change affects existing users. |
||
def get_related_serializer(self, instance, **kwargs): | ||
serializer_class = self.get_related_serializer_class() | ||
kwargs.setdefault('context', self.get_serializer_context()) | ||
return serializer_class(instance, **kwargs) | ||
|
||
def get_related_serializer_class(self): | ||
parent_serializer_class = super(RelatedMixin, self).get_serializer_class() | ||
|
||
if 'related_field' in self.kwargs: | ||
|
@@ -179,7 +184,8 @@ def get_related_field_name(self): | |
|
||
def get_related_instance(self): | ||
parent_obj = self.get_object() | ||
parent_serializer = self.serializer_class(parent_obj) | ||
parent_serializer_class = self.get_serializer_class() | ||
parent_serializer = parent_serializer_class(parent_obj) | ||
field_name = self.get_related_field_name() | ||
field = parent_serializer.fields.get(field_name, None) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.