Can I remove ORDER BY from a Django ORM query?

Actually, just do a query.order_by() is enough.

This is specified in the docs although it is a bit hard to find. The docs say:

If you don’t want any ordering to be applied to a query, not even the default ordering, call order_by() with no parameters.

Here is the implementation of order_by, for your reference –

def order_by(self, *field_names):
    """
    Returns a new QuerySet instance with the ordering changed.
    """
    assert self.query.can_filter(), \
        "Cannot reorder a query once a slice has been taken."
    obj = self._clone()
    obj.query.clear_ordering(force_empty=False)
    obj.query.add_ordering(*field_names)
    return obj

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)