gpt4 book ai didi

django-orm - 我可以从Django ORM查询中删除ORDER BY吗?

转载 作者:行者123 更新时间:2023-12-03 15:19:06 25 4
gpt4 key购买 nike

似乎Django默认情况下将ORDER BY添加到查询中。我可以清除吗?

from slowstagram.models import InstagramMedia
print InstagramMedia.objects.filter().query


SELECT
`slowstagram_instagrammedia`.`id`,
`slowstagram_instagrammedia`.`user_id`,
`slowstagram_instagrammedia`.`image_url`,
`slowstagram_instagrammedia`.`video_url`,
`slowstagram_instagrammedia`.`created_time`,
`slowstagram_instagrammedia`.`caption`,
`slowstagram_instagrammedia`.`filter`,
`slowstagram_instagrammedia`.`link`,
`slowstagram_instagrammedia`.`attribution_id`,
`slowstagram_instagrammedia`.`likes_count`,
`slowstagram_instagrammedia`.`type`
FROM
`slowstagram_instagrammedia`
ORDER BY
`slowstagram_instagrammedia`.`id`
ASC


```

最佳答案

您可以使用:查询中的clear_ordering方法

"""Removes any ordering settings. 

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


例:

>>> 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

关于django-orm - 我可以从Django ORM查询中删除ORDER BY吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18128610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com