首页 > 解决方案 > 无法按 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吗?

标签: djangodjango-countries

解决方案


推荐阅读