-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Allow Django 2.x path usage in routers #6148
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
Comments
Can you give an example of what the expected usage would look like? |
As of now we may write an action with
With path syntax it should look like:
Other possible examples on In the end, because |
To add to this, it there are other args that would be useful with routers (whether specified on the router or view). One I currently have a need for is custom path converter. If you were able to use the path route syntax to specify the url param used for detail lookups (e.g. |
I haven't spent too much time looking into it, but it should be possible for users to implement this as a custom router. You would need to do two things:
No changes to the |
Hi @rpkilby. I ran into this issue last night and it took more than an hour to figure out why my url wasn't being resolved correctly. Because all you get is the django debug page saying, 'I can't resolve this url, here are the patterns I tried', there's no obvious failure point to dig into, and no obvious place inside my code to insert a break point. I realize it's More Work for the DRF maintainers, but at the very least it should be recognized that DRF ideally should be fully compatible with the default Django path formatting. Otherwise this can result in extremely confusing bugs. |
This is addressed by the issue still being opened. |
I ran into a cousin of this: switching from manually made I've started work on a router using paths, where the type can be explicitly defined. |
|
Is this still a thing to be solved by #6789? |
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. |
yes |
Thanks @sevdog @auvipy for making this happened. I was also working on another solution for this problem with DRF version 3.13.1 before looking at #6789 and found out the change in PR is pretty radical: instead of having different URL Path formats for different ViewSets, we are modifying this behavior per-router. This will cause problems if we want to use Do we have a good reason for this approach? 'Cause I think we can do it more flexibly and with less changes by just overriding def get_lookup_regex(self, viewset, lookup_prefix=''):
lookup_value_regex = getattr(viewset, 'lookup_value_regex', None)
lookup_value_converter = getattr(viewset, 'lookup_value_converter', None)
assert not lookup_value_regex or not lookup_value_converter, (
'lookup_value_regex and lookup_value_converter are mutually exclusive'
)
if lookup_value_converter:
base_regex = '<{lookup_value}:{lookup_prefix}{lookup_url_kwarg}>'
lookup_field = getattr(viewset, 'lookup_field', 'pk')
lookup_url_kwarg = getattr(viewset, 'lookup_url_kwarg', None) or lookup_field
return base_regex.format(
lookup_prefix=lookup_prefix,
lookup_url_kwarg=lookup_url_kwarg,
lookup_value=lookup_value_converter
)
return super().get_lookup_regex(viewset, lookup_prefix) |
I see, thanks. |
Checklist
Proposal
In Django 2.x the new
path
(andre_path
) were introduced to simplify URL routing.It could be nice to have the ability to use path to simplify route annotation (aka
@action
).This however may break compatibility with Django 1.x which should be tested.
The text was updated successfully, but these errors were encountered: