Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

filter form not rendering on UI of drf if changes value in Response class. #9653

Unanswered
sainipray asked this question in General
Discussion options

Suppose this is a scenario where I'm generating data for a chart by aggregating data with filters and searching in GenericAPIView.

class AnalyticsView(GenericAPIView):
 filterset_class = OrderFilter
 filter_backends = [DjangoFilterBackend, SearchFilter]
 queryset = Order.objects.select_related('delivery', 'platform')
 
 def get(self, request, *args, **kwargs):
 queryset = self.filter_queryset(self.get_queryset())
 monthly_data = queryset.annotate(
 month=TruncMonth('date_of_sale')
 ).values('month').annotate(
 total_quantity=Sum('quantity_sold')
 ).order_by('month')
 
 data = [
 {
 'month': item['month'].strftime("%Y-%m"),
 value_field: item['total_quantity']
 }
 for item in queryset
 ] 
 return Response({'results': data })

So here Filter UI is not showing in the DRF template, because {'results': data }

and DRF have

 def get_filter_form(self, data, view, request):
 if not hasattr(view, 'get_queryset') or not hasattr(view, 'filter_backends'):
 return
 # Infer if this is a list view or not.
 paginator = getattr(view, 'paginator', None)
 if isinstance(data, list):
 pass
 elif paginator is not None and data is not None:
 try:
 paginator.get_results(data)
 except (TypeError, KeyError):
 return
 elif not isinstance(data, list):
 return
 

this method returns None that's why UI filter UI not rendering.

I think it should not depend on the data instance.

It's my thought, let me know if I'm wrong in this.

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /