Hi all,

I'm in the situation where I'd like to join two .order_by() calls on a 
QuerySet without losing the ordering set by the first call.

This was formerly discussed in https://code.djangoproject.com/ticket/9415 . 
I agree that simply changing the current behavior is not going to fly due 
to its backwards incompatibility.

My proposed API is similarly to force_insert/force_update on Model.save():

class QuerySet:
    def order_by(self, *field_names, append=False, prepend=False):
        if append and prepend:
            raise AssertionError('Can only append or prepend, not both')
        assert self.query.can_filter(), \
            "Cannot reorder a query once a slice has been taken."
        obj = self._chain()
        if not append and not prepend:
            obj.query.clear_ordering(force_empty=False)
        obj.query.add_ordering(*field_names, prepend=prepend)
        return obj


class Query:
    def add_ordering(self, *ordering, prepend=False):
        if append and prepend:
            raise AssertionError('Can only append or prepend, not both')
        # ...
        if ordering:
            if prepend:
                self.ordering = ordering + self.ordering
            else:
                self.order_by += ordering
        # ...

I'm happy to open a ticket once I got some feedback.

Cheers,

Markus

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/c9279d82-6b81-42cb-8577-16004e623e3d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to