Django:复制和操作查询集?
我需要重新排序 Django 查询集,因为我想放置 FloatField 上的 ORDER BY DESC 查询底部没有值。
不幸的是,我正在努力寻找一种优雅的方法来操作 Django 查询集。这是我到目前为止所得到的:
cities = City.objects.filter(country__id=country.id).order_by('value')
if cities.count() > 1:
cities_sorted = cities
del manors_sorted[:]
for city in cities:
cities_sorted += city
# Add code to
cities = cities_sorted
目前,此操作失败,并显示 'QuerySet' 对象不支持项目删除
。
知道如何复制并重新排序此查询集以将“无”项目放在最后吗?
I need to re-order a Django queryset, because I want to put None values at the bottom of an ORDER BY DESC query on a FloatField.
Unfortunately, I'm struggling to find an elegant way to manipulate a Django queryset. Here's what I have so far:
cities = City.objects.filter(country__id=country.id).order_by('value')
if cities.count() > 1:
cities_sorted = cities
del manors_sorted[:]
for city in cities:
cities_sorted += city
# Add code to
cities = cities_sorted
Currently, this fails with 'QuerySet' object does not support item deletion
.
Any idea how I can copy and re-order this QuerySet to put None items last?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您例如,查询集将被评估为一个列表。对其调用
list()
:The queryset will be evaluated to a list, if you eg. call
list()
on it: