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

wq.db.rest.serializers.ModelSerializer's context contains no request #70

Closed
tomaszn opened this issue Sep 27, 2017 · 4 comments
Closed
Milestone

Comments

@tomaszn
Copy link
Contributor

tomaszn commented Sep 27, 2017

My serializer should calculate some values which depend on the user's account.

I subclassed ModelSerializer (from wq), and can't find a way to reliably determine the request in the constructor (to prepare information for to_representation method to use for every serialized instance).

According to https://stackoverflow.com/a/34617511/1762343, the request should be in self.context dict, but I can only (sometimes) find 'router': <wq.db.rest.routers.ModelRouter> there.

@sheppard
Copy link
Member

The router initializes the serializers at startup to generate the configuration object. So, there isn't any request in the context at that time. But once you get to actually rendering and parsing records the request object should be there. So, you just need to have a check or a fallback when dealing with code that relies on request. One way to do this would be to wait until to_representation to load the user information.

@tomaszn
Copy link
Contributor Author

tomaszn commented Sep 29, 2017

I see now that the request object is missing in the list queries for parent objects; the context is initialized with the router only:

return serializer(obj, many=many, context={'router': self}).data

You are right for the simple queries.

@tomaszn
Copy link
Contributor Author

tomaszn commented Nov 12, 2018

This way I managed to add context.request to serializers:
https://stackoverflow.com/a/31724701/1762343

@sheppard
Copy link
Member

sheppard commented Apr 5, 2019

I added the request to the context for parent objects.

@sheppard sheppard added this to the 1.1 milestone Dec 2, 2020
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

No branches or pull requests

2 participants