class ExportTabView(ModelViewSet):
permission_classes = [UserHasDatasetChangeAccess]
queryset = JobTable.objects.all()
filterset_fields = [
"job_type",
"action",
"source_table__name",
"source_table__type",
]
ordering_fields = ["created_on", "modified_on"]
ordering = ["-modified_on"]
I am able to use django filters with above api as :
/api/export-tab-view?source_table__name='temp'
But after adding custom urls in this model view set, I am not able to use django filters .
class ExportTabView(ModelViewSet):
serializer_class = ExportTabSerializer
permission_classes = [UserHasDatasetChangeAccess]
queryset = JobTable.objects.all()
filterset_fields = [
"job_type",
"action",
"source_table__name",
"source_table__type",
]
ordering_fields = ["created_on", "modified_on"]
ordering = ["-modified_on"]
@action(detail=False)
def export_dataset(self, request, id=None):
"""
Returns a list of all the relationship names that the given
dataset has
"""
jobs = JobTable.objects.filter(
dataset=get_object_or_404(DataSet, id=self.request.META.get(DATASET_ID, ""))
)
return Response(ExportTabSerializer(jobs, many=True).data)
Now filters are not working when i call api
/api/export-tab-view/export_dataset?source_table__name='temp'
CodePudding user response:
You can filter through the dataset by doing this, but it depends on which field of the DataSet object that you are hopping to query! Am guessing if you want to make queryset of The DataSet model assuming the field name is dataset, supply it to the filter just like so:
@action(detail=False)
def export_dataset(self, request, id=None):
"""
Returns a list of all the relationship names that the given
dataset has
"""
dataset=get_object_or_404(DataSet id=self.request.META.get(DATASET_ID, ""), **dict(request.query_params.items()))
jobs = JobTable.objects.filter(dataset=dataset)
return Response(ExportTabSerializer(jobs, many=True).data)
CodePudding user response:
I have done this way and its working fine for now.
@action(detail=False)
def export_dataset(self, request, id=None):
"""
Returns a list of all the relationship names that the given
dataset has
"""
jobs = JobTable.objects.filter(
dataset=get_object_or_404(DataSet, id=self.request.META.get(DATASET_ID, "")),
**dict(request.query_params.items()))
return Response(ExportTabSerializer(jobs, many=True).data)