gpt4 book ai didi

python - django 中的自定义订单

转载 作者:行者123 更新时间:2023-11-30 23:04:52 24 4
gpt4 key购买 nike

我必须在我的代码中进行自定义排序,

考虑下面的例子:

class Car(models.Model):    
type
brand

每当我使用品牌订购时,我都会得到以下查询集

cars = ["Benz","BMW","Ford","Honda","Suzuki"]

I need to do order by brand in which "BMW" should always come at last like below queryset,

cars = ["Benz","Ford","Honda","Suzuki", "BMW"]

注意:我不想使用/将其转换为列表。我需要在 order by 之后对此执行一些查询集操作(如values_list)。

最佳答案

你可以尝试这样做:

https://stackoverflow.com/a/2176471/4971083

这是在 Django 中按特定值对记录进行排序的解决方案。

car = Car.objects.filter(product__id=product_id).extra(
select={'is_top': " brand='BMW'"})
car = car.extra(order_by = ['is_top'])

关于python - django 中的自定义订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33493185/

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