-
Notifications
You must be signed in to change notification settings - Fork 23
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
Pass additional kwargs to nested serializers #48
Comments
Hi @philipstarkey, sorry for the late reply, was on vacation. I must confess, having lived in Flask-land for such a period of time, I'm returning to this unsure as to what can be achieved through the serializer kwargs. Your proposal sounds completely reasonable though. When you say |
Hi @evenicoulddoit, no worries! Specifically in my case, I want to pass And yep, my thought was to provide the parent serializer. Ideally I guess you would be able to specify:
I'd then expect to be able to access the request from the Or, if you don't need something that fancy:
(The latter I guess you could achieve by using a different serializer class for your nested serializer, but it's really the first example I care about!) |
@philipstarkey I'm sorry it took me so long to look into this. You mentioned specifically the issue of accessing the request context from within a nested serializer. In this branch, I've attempted to create a test to cover that exact situation. I created an expandable model field, and the nested model serializer has some additional fields which both pull from the context successfully. Did I misinterpret your question? |
@evenicoulddoit Sorry for the delay in replying and thanks for creating that test case. That does appear to cover the cases I intended. I'm surprised it worked because I was sure I had tested something similar and it didn't. I've got a deadline for some other stuff coming up, but in a couple of weeks I'll be diving back into DRF/serializer extension things and can investigate more closely to see if the issue was my user error or if there's a bug when additional complexity is present. |
@philipstarkey perfect, thanks for the update - good to know I understood your issue correctly at least! |
I managed to look into this a bit more today. It seems the context is not available in In addition I realised there may also be a distinction between context available in |
Further to this, it seems that DRF itself has the convention that the context is not available in nested serializers until the parent serializer calls If you think my request is thus out of scope for this project as it's a DRF convention, I'd be happy to accept that and to close the issue (I have sufficient workarounds in place at the moment) |
I've run into a problem where some custom logic in a nested serializer is falling over because I can't provide custom kwargs (in this case, what I want to pass down is really just a subset of the outer serializers
self.context
).I don't think this functionality exists at the moment (could be wrong) based on this:
django-rest-framework-serializer-extensions/rest_framework_serializer_extensions/serializers.py
Lines 649 to 658 in 945e33d
I think the ideal solution would be to add an additional option to
Meta.expandable_fields
that is used as the base for the serializer kwargs if it's a dictionary, or if it's a callable, call it passingself
as the only arg and use the result of the call as the base forkwargs
(so that you can control what gets passed down from one serializer to another with custom, and possibly complex, logic). Does that sound feasible and a good idea?The text was updated successfully, but these errors were encountered: