django - 无法按 country__name 对查询集进行排序
问题描述
我正在使用django-countries在我的 django 模型中设置一个国家:
# models.py
from django_countries.fields import CountryField
class MyModel(models.Model):
title = models.CharField(max_length=255, blank=True)
country = CountryField()
我想要一个由 排序的查询集country.name
,但显然是以下查询集:
MyModel.objects.live().order_by('country__name')
不起作用:
Cannot resolve keyword 'name' into field. Join on 'country' not permitted.
此外,显然无法使用模型Meta
选项ordering
。
我可以通过以下方式在模板中对其进行排序:
{% for item in items.all|dictsort:"country.name" %}
但更愿意在查询集中完成此操作。
亲爱的互联网,你知道让这个查询集排序的方法country.name
吗?
解决方案
推荐阅读
- django - Django - 检查是否点击了表单提交按钮?
- r - 如何通过特定前缀将多个数据框转换为列表
- openscenegraph - OSG 轮廓仅出现在几何图形的一侧
- c - 从c中的特定接口发送数据包
- r - 使用 POSIXct 为 left_join() 格式化两个不同的数据帧
- angular - Angular 7:core.js:12501 错误错误:未捕获(承诺):错误:无法匹配任何路由。网址段:
- c# - WaitForSeconds 在“自定义”协程中不起作用
- docker - 为什么服务名称更改后未在 docker-compose ps 中列出?
- android - 间歇性 FirebaseFirestoreException:PERMISSION_DENIED:缺少权限或权限不足
- html - line-height:导致行前和行后不被分割(用于良好的排版)