-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Router accepts app_name to support hyperlinked relations in issue #5850 #5851
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
Conversation
Regarding the wip commit: I tried to add an additional test case that requires
The URLCONF in tests normally points at a project-level |
Compared to PR #5609 this is a smaller change, with no assumptions about the desired namespace. Nominating the Router to track the desired |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @shuckc
Thanks for the input here.
A few thoughts:
- This isn't a new issue with Django 2.0. The Hyperlinked fields have never been compatible with namespaces. (There's a reason for that. This has been thought about.)
- The use of
app_name
is not compulsory, even with Django 2.0. (It's just compulsory if you want to usenamespace=
) - You're best bet here is to not namespace your DRF URLs. (How many times will
%(model_name)-detail
actually lead to collisions in your project?) - If you need namespacing, your best bet is to pass the
view-name
by hand. (Either declare the fields or useextra_kwargs
.) - The trouble with the suggestion here is that it's breaking the layering of your application. You're passing a reference to your router back down to your viewset. (The viewset should know nothing about the router.)
- Even doing that, this doesn't work: if I have a foreign key to a model in a different app I get the wrong URL.
Hi Carlton, 2/3. Excellent - this helps, thank you. I can remove
|
Oh, wow. I see. If that's what you have in mind it's just the same as always requiring the same namespace (for which not requiring any namespace would be the way to go). I'm going to close this off. I'll review #5850 for a possible docs tweak but this is essentially a (long-standing) known limitation. |
There is a great deal of utility provided by
HyperlinkedModelSerializer
working out of the box with the now mandatoryapp_name
. This PR supports this by providing anapp_name
argument to theSimpleRouter
. The router is tagged on each view generated whilst expanding the ViewSet classes. Theapp_name
is then added into the request object at the head of view rendering and retrieved whilst reversing URLs in the serializers.A failing test is provided to confirm the existing situation, and a passing test for the new case. Flake8 is clean and no existing tests were changed.