Skip to content

Document how to transform factory request to DRF request - #9380

Merged
auvipy merged 1 commit into
encode:mainfrom
mgaligniana:add-documentation-to-transform-factory-request-to-drf-request
Aug 16, 2025
Merged

Document how to transform factory request to DRF request#9380
auvipy merged 1 commit into
encode:mainfrom
mgaligniana:add-documentation-to-transform-factory-request-to-drf-request

Conversation

@mgaligniana

@mgaligniana mgaligniana commented Apr 12, 2024

Copy link
Copy Markdown
Contributor

Description

Hi there!

As there were many issues opened around this topic: "APIRequestFactory does not return a Request object" I think it would be worth to have a place in the documentation where explains it.

Following the Tom's comment in the last issue #6488 (comment):

If someone's invested and there's clearly a good existing place in the docs to highlight this, then we could accept a docs PR here.

*Past issues related: #4440 and #3608

This is what my proposal looks like:

image

I've also added a test to follow the Carlton's comment #6488 (comment)

Thank you!

@mgaligniana

Copy link
Copy Markdown
Contributor Author

Hi @carltongibson! Is something like that the documentation you had in mind?

@carltongibson carltongibson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hi @mgaligniana, it's similar!

I think people do this when they're testing individual view methods, without going via dispatch.

So, I'd be inclined to say, and show, that. I.e. what you have plus initial() and calling a (fictional) view method, which would be the point of the test.

Make sense?

But, yes, great.

@stale

stale Bot commented Apr 27, 2025

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the stale label Apr 27, 2025
@auvipy auvipy removed the stale label Apr 28, 2025
Comment thread docs/api-guide/testing.md Outdated
request.user = user
response = view(request)

In case you want to test the request having a REST famework's `Request` you have to transform it by-hand before:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we can rewrite the text

Comment thread docs/api-guide/testing.md Outdated
@stale

stale Bot commented Jun 27, 2025

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the stale label Jun 27, 2025
@ulgens

ulgens commented Jun 27, 2025

Copy link
Copy Markdown
Contributor

I think this is still relevant and shouldn't be marked as stale.

@mgaligniana

Copy link
Copy Markdown
Contributor Author

Hi ulgens! Yes, auvipy fixed the text I added but let me know if there is other change required!

@stale stale Bot removed the stale label Jun 27, 2025
@ulgens

ulgens commented Jun 27, 2025

Copy link
Copy Markdown
Contributor

@mgaligniana No change requests & recommendations on my end. I just saw the notification from the stale bot and thought that this is better merged than lost.

Comment thread tests/test_testing.py
Comment thread tests/test_testing.py Outdated

@deepakangadi deepakangadi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the clarifications

Comment thread docs/api-guide/testing.md Outdated
Comment on lines +107 to +120
class DummyView(APIView):
...

factory = APIRequestFactory()
request = factory.get('/', {'demo': 'test'})
DRF_request = DummyView().initialize_request(request)
assert DRF_request.query_params == {'demo': ['test']}

request = factory.post('/', {'example': 'test'})
DRF_request = DummyView().initialize_request(request)
assert DRF_request.data.get('example') == 'test'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It seems to work by using the APIView directly, let's not get distracted with a subclass, shall we? Also, let's try to follow pep8 (lowercase snake case for variables):

Suggested change
class DummyView(APIView):
...
factory = APIRequestFactory()
request = factory.get('/', {'demo': 'test'})
DRF_request = DummyView().initialize_request(request)
assert DRF_request.query_params == {'demo': ['test']}
request = factory.post('/', {'example': 'test'})
DRF_request = DummyView().initialize_request(request)
assert DRF_request.data.get('example') == 'test'
factory = APIRequestFactory()
django_request = factory.get('/', {'demo': 'test'})
request = APIView().initialize_request(django_request)
assert request.query_params == {'demo': ['test']}
django_request = factory.post('/', {'example': 'test'})
request = APIView().initialize_request(django_request)
assert request.data.get('example') == 'test'

Same simplification for the test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you @browniebroke! I've applied the new variable name.

As we use to extend from APIView I wanted to do a "real world example" for that was the DummyView. I've added a comment to mean that, but if the team is not agree I can delete it for sure!

Comment thread tests/test_testing.py Outdated
@mgaligniana
mgaligniana force-pushed the add-documentation-to-transform-factory-request-to-drf-request branch 3 times, most recently from 7f0f475 to 09399e9 Compare July 26, 2025 14:09
Comment thread tests/test_testing.py

def test_transform_factory_django_request_to_drf_request(self):
"""
ref: GH-3608, GH-4440 & GH-6488.

@mgaligniana mgaligniana Jul 26, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've added references here. Not sure if we should add them to the new section of the documentation?

@mgaligniana

mgaligniana commented Jul 26, 2025

Copy link
Copy Markdown
Contributor Author

Ready for review! Thanks for all yours comments and the pacience team!

@deepakangadi deepakangadi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! 馃憤馃徎

@mgaligniana
mgaligniana force-pushed the add-documentation-to-transform-factory-request-to-drf-request branch from 09399e9 to 3b6e995 Compare July 26, 2025 14:18
@browniebroke browniebroke added this to the 3.16 milestone Aug 9, 2025
@auvipy
auvipy merged commit 503f060 into encode:main Aug 16, 2025
7 checks passed
@browniebroke browniebroke modified the milestones: 3.16, 3.17 Sep 14, 2025
@browniebroke browniebroke changed the title Add documentation about how to transform factory request to DRF request Document how to transform factory request to DRF request Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

6 participants