Any methods can be added to the view set using a ViewParams class. To add a method, an element of dictionary returned by getParams() method of the ViewParams class should have a key as name of the method (str) and the value should be reference to the function object.
For example, to use custom list() method instead of the one given by django-to-rest something similar to below example can be used:
fromto_restimportconstantsfromto_rest.utilsimportViewParamsfromtest_basicsimportmodels# Here, test_basics is the app directoryclassCustomListMethod(ViewParams):defgetParams():deflist(self,request,*args,**kwargs):objects=models.StudentWithCustomMethod.objects.filter(year=2)serializer=self.get_serializer(objects,many=True)returnResponse(serializer.data)temp=dict()temp['list']=listreturntemp
Note
The name of this class has to be passed to the decorator in models.py.
Similarly, any method can be added to the view set.
fromto_restimportconstantsfromto_rest.utilsimportViewParamsfromtest_basicsimportmodels# Here, test_basics is the app directoryfromrest_framework.responseimportResponsefromrest_framework.decoratorsimportactionclassCustomAction(ViewParams):defgetParams():defcustomaction(self,request,pk=None):obj=models.StudentWithCustomAction.objects.get(pk=pk)returnResponse({'msg':"custom action working for "+obj.name})customaction=action(detail=True,methods=['get'],url_name='customaction')(customaction)temp=dict()temp['customaction']=customactionreturntemp