Allow mocking of Request objects #7822
Replies: 11 comments
-
I finally figured this out. For some reason you must manually set the version and the versioning_scheme:
This seems to work, but the HyperLinkRelated values in the JSON that is serialized have the server set to
But I also need to set I don't know if this is now a documentation issue or a bug. Either would be helpful for future people. |
Beta Was this translation helpful? Give feedback.
-
This is an awkward thing to do yes. Hyperlinked relationships plus versioning in the URL doesn't work terribly well if you want to mock up requests and use them outside of a normal view context. (Eg. the versioning scheme only gets applied to a request at the point of a view) The best way for us to resolve this would be to make it easier to mock |
Beta Was this translation helpful? Give feedback.
-
I've retitled this issue accordingly. |
Beta Was this translation helpful? Give feedback.
-
That sounds like a good strategy. What made sense to me at one point was also to use the |
Beta Was this translation helpful? Give feedback.
-
Indeed yes - the mocked request would need to include an appropriate path if path based versioning is being used, in order for serializers to correctly reverse the resulting URLs. |
Beta Was this translation helpful? Give feedback.
-
And apologies for your pain on this - you hit an awkward corner there! :-/ |
Beta Was this translation helpful? Give feedback.
-
Some notes to other desperate sweepers of awkward corners:
from django.test import RequestFactory
request = RequestFactory().request(HTTP_HOST='localhost:8001')
serializer = AssetSerializer(context={'request': request})
obj_json = serializer.to_representation(obj)
from django.contrib.sites.models import Site
http_host = Site.objects.get_current().domain |
Beta Was this translation helpful? Give feedback.
-
Note to self - work from #5609 might be relevant. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to follow this to render data via a class that implements ListModelMixin and this seems even more tricky, I can't work out how to pass my queryset to the .list() method and get something out the other side. |
Beta Was this translation helpful? Give feedback.
-
Looking at the docs I realised i needed to call import sys
from display_xml import XML
from workbaskets.views import *
items_list = ItemsViewSet.as_view({'get': 'list'})
response = items_list(request, workbaskets, format='xml')
print(response.rendered_content) |
Beta Was this translation helpful? Give feedback.
-
I posted about this on the mailing list and in IRC. I've been trying to serialize a model that has a few HyperLinkRelated fields, and I get the impression it's very hard.
The drf documentation on serializers has a section that says you can simply do:
Unfortunately, this doesn't work for HyperLinked relationships. When you try to do it with them, you get something like:
So, I figured out I can add context attribute, like:
Which then returns the error:
On IRC, somebody suggests a crazy fix involving patching the
reverse
method of the serializer:I could do that, but I'd love if there was a better solution. I think this bug request is basically:
Thanks,
Mike
Beta Was this translation helpful? Give feedback.
All reactions