Can I remove ORDER BY from a Django ORM query?

后端 未结 3 1615
轻奢々
轻奢々 2021-01-17 08:47

It seems like Django by default adds ORDER BY to queries. Can I clear it?

from slowstagram.models imp         


        
相关标签:
3条回答
  • 2021-01-17 09:11

    You can use: clear_ordering method from query

    """Removes any ordering settings. 
    
    If 'force_empty' is True, there will be no ordering in the resulting
    query (not even the model's default).
    """
    

    Example:

    >>> from products.models import Product
    >>> products = Product.objects.filter(shortdesc='falda').order_by('id')
    >>> print products.query
    SELECT "products_product"."id", "products_product"."shortdesc"
    WHERE "products_product"."shortdesc" = falda
    ORDER BY "products_product"."id" ASC
    >>> products.query.clear_ordering()
    >>> print products.query
    SELECT "products_product"."id", "products_product"."shortdesc"
    WHERE "products_product"."shortdesc" = falda
    
    0 讨论(0)
  • 2021-01-17 09:12

    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
    
    0 讨论(0)
  • 2021-01-17 09:22

    Try to use .order_by('?') at the end of queryset.

    0 讨论(0)
提交回复
热议问题