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

Change the URL parameter names? #130

Open
WilliamMayor opened this issue Nov 30, 2018 · 4 comments
Open

Change the URL parameter names? #130

WilliamMayor opened this issue Nov 30, 2018 · 4 comments

Comments

@WilliamMayor
Copy link

I suspect this is a silly question that I could answer myself if I could find the right parts of the docs. So sorry in advance about that.

I want to have my URLs be more explicit about the path params. This is mostly for people reading the auto generated swagger docs.

I'd like my URLs to look like this:

/node/
/node/{node_id}/
/node/{node_id}/disk/
/node/{node_id}/disk/{disk_id}/

The closest I can get is:

/node/
/node/{node_id}/
/node/{node_node_id}/disk/
/node/{node_node_id}/disk/{disk_id}/

I get this far by setting the lookup field in my NodeViewSet to 'node_id', in DiskViewSet it's 'disk_id'.

My router then looks like this:

router = routers.SimpleRouter()
router.register(r"node", views.NodeViewSet, base_name="node")
node_router = routers.NestedSimpleRouter(router, r"node", lookup="node")
node_router.register(r"disk", views.DiskViewSet, base_name="node-disk")

How can I get rid of that pesky 'node_' prefix? Thanks!

@ghost
Copy link

ghost commented Jul 15, 2019

If you look in the source, the nested router classes include some info on the lookup argument:

lookup:
        The regex variable that matches an instance of the parent-resource
        will be called '<lookup>_<parent-viewset.lookup_field>'
        In the example above, lookup=domain and the parent viewset looks up
        on 'pk' so the parent lookup regex will be 'domain_pk'.
        Default: 'nested_<n>' where <n> is 1+parent_router.nest_count

Basically there will always be two strings separated by an underscore (see routers.py line 53)

In your case the Viewset's lookup_field is likely node_id, which when combined with the router's lookup='node' results in that node_node_id string 🙂

@matthewsheeran
Copy link

matthewsheeran commented Apr 18, 2023

The question is how do you fix i.e. change this!?

@alanjds
Copy link
Owner

alanjds commented Apr 19, 2023

Hi @matthewsheeran.

I think that your question is similar to the one on #270, yet idk if is a duplicate.
Anyway, there I dug over why stuff is like that. You can read more here: #270 (comment)

Hope this to helps to understand how and why things are as they are today, yet any change proposal will be very welcome from my side 👍

Best regards.

@matthewsheeran
Copy link

matthewsheeran commented Apr 20, 2023

Yeah i tried changing things in the NestedMixin etc code but only succeeeded breaking things as you cautioned. I then looked at only modifying the Swagger UI from drf-spectacular - which was the real issue - along the lines of tfranzel/drf-spectacular#516 . Not exaclty per this post but something similar for drf-yasg with their respective SchemaGenerator and OpenAPISchemaGenerator being customized which sounds more promising for my purposes.. Ah here it is: #180

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

3 participants