django-url-filter icon indicating copy to clipboard operation
django-url-filter copied to clipboard

Sorting support

Open miki725 opened this issue 9 years ago • 3 comments

Having form validate all of the query data, might as well add ordering support.

miki725 avatar Oct 20 '15 17:10 miki725

I'm using this mixin for ordering.

class QueryOrderableViewSetMixin(object):

    def get_queryset(self):
        queryset = super(QueryOrderableViewSetMixin, self).get_queryset()
        try:
            order_by = self.request.query_params['order_by']
            return queryset.order_by(*order_by.split(','))
        except KeyError:
            return queryset

haakenlid avatar Nov 09 '15 14:11 haakenlid

Is there a particular reason that DRF's built in ordering filter_backend fails in conjunction with using Django URL filter?

Here is my class-based view:

class OpportunityListCreate(generics.ListCreateAPIView):
    """
    Class-based view supporting get and post request to list all Opportunity or create a Opportunity, if permitted.

    Attributes:
        authentication_class (tuple): Uses JWT authentication.
        permission_classes (tuple): Requires authentication.
        queryset (method_call): Method returning list for use in serializing results for response.
        serializer_class (cls): Serializer class providing serialized UserProfile for response.
    """
    authentication_classes = (JSONWebTokenAuthentication,)
    permission_classes = (IsAuthenticated,)
    queryset = Opportunity.objects.all()
    filter_backends = (DjangoFilterBackend,
                       OpportunityFilterBackend, filters.OrderingFilter,)
    ordering_fields = ('created_date',)
    filter_fields = ['tags', 'created_date']
    serializer_class = OpportunitySerializer

I am using DjangoFilterBackend, my own OpportunityFilterBackend to restrict querysets based on the user type, and filters.OrderingFilter to provide ordering support. However, ordering does not seem to work at all.

Where does one employ the mixin used above?

cmuell89 avatar Jul 18 '17 16:07 cmuell89

I'm really interested in this feature as well.

aslebloas avatar Feb 15 '20 23:02 aslebloas