Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting support #10

Open
miki725 opened this issue Oct 20, 2015 · 3 comments
Open

Sorting support #10

miki725 opened this issue Oct 20, 2015 · 3 comments

Comments

@miki725
Copy link
Owner

miki725 commented Oct 20, 2015

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

@haakenlid
Copy link
Contributor

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

@cmuell89
Copy link

cmuell89 commented Jul 18, 2017

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?

@aslebloas
Copy link

I'm really interested in this feature as well.

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

No branches or pull requests

4 participants