Skip to content
Advertisement

How do I specify a custom lookup field for a DRF action on a viewset?

I would like to specify a custom lookup field on the action (different from the viewset default “pk”), i.e.

JavaScript

But the router does not generate the correct urls:

JavaScript

yields url:

JavaScript

instead of

JavaScript

I do not want to change the lookup field for the whole viewset though and have been unsuccessful in finding a way to do this for the action itself after debugging through the router url generation. I did notice that model viewsets have this method:

JavaScript

but am unsure how to get it to be called to generate custom urls or if it is even relevant. Any help would be great thanks!

Advertisement

Answer

I think it will create much confusion for your API consumers if you have 2 different resource identification on the same resource.

You can name that action query_by_uuid or just allow them to use list_view to filter by uuid if you only want to represent the object tho. (so consumers can use /test/?uuid= to retrieve data)

But if you really want to do it, you can simply override get_object method to filter for your custom action tho:

JavaScript

Here is a bit hacky solution for generate uuid in router with detail=False.

JavaScript
Advertisement