Accessing specific fields with Rest Framework ViewSets


I am new to the Django Rest Framework and have been recently introduced to Viewsets. I understand that Viewsets provide prebuilt classes for handling commonly used CRUD operations. This in turn allows developers to configure their views a lot more quickly and efficiently by simply initializing a couple of variables in their View Classes. Form what I have understood so far is that, for example, if we are using a ReadOnlyModelViewSet, then our class will automatically generate endpoints for GET requests. And by default, if we wish to perform a GET operation where we would like to retrieve a specific row from our table, then the ModelViewSet will use the DB models pk to do so. My question is this. What if I would like to access a db column with a field other than the pk. Would I have to override a default pre-existing ReadOnlyModelViewSet method to do so?

Hey there!

I think that what you mean from this is related with lookup_field, docs.
You can set it on your viewset.
Take into account that this can have perfomance implications on a bid dataset if you filter from a db column not indexed.

Thank you for the response!