tombreit
tombreit

Reputation: 1369

Unable to sort queryset by country__name

I'm using django-countries to set a country in my django model:

# models.py
from django_countries.fields import CountryField

class MyModel(models.Model):
    title = models.CharField(max_length=255, blank=True)
    country = CountryField()

I would like a have a queryset ordered by country.name, but apparently the following queryset:

MyModel.objects.live().order_by('country__name')

does not work:

Cannot resolve keyword 'name' into field. Join on 'country' not permitted.

Further it is - apparently - not possible to use the model Meta option ordering.

I could get it ordered in the template by: {% for item in items.all|dictsort:"country.name" %} but would prefer to get this done in the queryset.

Did you, dear internet, are aware of a way to get this queryset orderd by country.name?

Upvotes: 1

Views: 369

Answers (0)

Related Questions