title: Django custom ordering in url queries
body: <p><strong>Update Two</strong></p>
  
  <p>So unfortunately, @Reza Torkaman Ahmadi's idea didn't work out in the end. This is because our program has a filtering function that relies on get_queryset, and overriding the get_queryset method in our views was messing up that function. </p>
  
  <p>So, my partner and I discussed it, and here's what he came up with.</p>
  
  <p><code>
  class OrderbyFilter(filters.OrderingFilter):
      def get_ordering(self, request, queryset, view):
          """
          Ordering is set by a comma delimited ?$orderby=... query parameter.
          Extends the OrderingFilter of the django rest framework to redefine
          ordering parameters to "asc" and "desc".
          """
          params = request.query_params.get(self.ordering_param)
          if params:
              field_queries = [param.strip() for param in params.split(',')]
              fields = []
              for field_query in field_queries:
                  field_query = field_query.split()
                  if len(field_query) &lt;= 2:
                      while "asc" in field_query:
                          field_query.remove("asc")
                      for i, field in enumerate(field_query):
                          if field == "desc":
                              field_query[i-1] = "-" + field_query[i-1]
                      while "desc" in field_query:
                          field_query.remove("desc")
                      fields.append(field_query[0])
                  else:
                      fields.append([param.strip() for param in params.split(',')])
              ordering = self.remove_invalid_fields(queryset, fields, view, request)
              if ordering:
                  return ordering
          return self.get_default_ordering(view)
  </code></p>
  
  <p>Basically, this function over-rides Django REST's source code, specifically the get_ordering function in the OrderingFilter. What it does is, if 'asc' is in the query after the field, it removes it and treats it like normal (for normal ascension ordering)</p>
  
  <p>Otherwise if 'desc' is there, it removed the 'desc', and applies a hyphen.</p>
  
  <p><strong>Update</strong> Answered. Applied @Reza Torkaman Ahmadi idea and it works great after modifying it to fit my needs. Thanks mate!</p>
  
  <p>Currently, in Django API rest framework, if a user wants to see a list of something in ascending or descending order, they have to do the following:</p>
  
  <p>'Datastream&amp;order_by=-name' shows names in descending order
  'Datastream&amp;order_by=name' shows names in ascending order.</p>
  
  <p>I want to make a custom query where typing in 'Datastream&amp;order_by=asc name' will order the names by ascending order, and 'desc name' will do so in descending order. </p>
  
  <p>I've looked at some of the source code for the REST framework, but I may be looking in the wrong area. Getting stumped on what I should do. Any ideas?</p>