EDIT: I just noticed that the below described behavior only appears on my dev machine and not on the production server. On the productin server it is ordered as desired. It must be something in the settings of my PostgreSQL servers.
I have a Django app with the following models that have the ordering set with class Meta
:
class Akutsomatik(models.Model):
bereichname = models.CharField(max_length=200)
class Meta:
ordering = ['bereichname']
def __str__(self):
return self.name
# This model is only included in the example for better understanding
class Klinik(models.Model):
name = models.CharField(max_length=200)
akutsomatik = models.ManyToManyField(Akutsomatik, blank=True)
def __str__(self):
return self.name
And my template:
{% if klinik.akutsomatik.all %}
<ul>
{% for akutsomatik in klinik.akutsomatik.all %}
<li>{{ akutsomatik }}</li>
{% endfor %}
</ul>
{% endif %}
The problem is that the data of my app is in German and the ordering is set to UTF-8. The output would look something like that:
Augenchirurgie / Ophtalmologie
Brustchirurgie
Viszeralchirurgie
Wiederherstellende Chirurgie
Ästhetische Chirurgie
but in German it should be like:
Ästhetische Chirurgie
Augenchirurgie / Ophtalmologie
Brustchirurgie
Viszeralchirurgie
Wiederherstellende Chirurgie
Note that Ästhetische Chirurgie
should come before Augenchirurgie / Ophtalmologie
.
Does anyone know if there is a way to set the collation in class Meta
? I tried the following as decribed here:
from django.db.models import Func
class Meta:
ordering = [Func('bereichname',
function='utf8_general_ci',
template='(%(expressions)s) COLLATE "%(function)s"')
]
But I get the following template error:
collation "utf8_general_ci" for encoding "UTF8" does not exist
LINE 1: ...ER BY ("klinik_finder_akutsomatik"."bereichname") COLLATE "u...
I also tried to set function
to function='de_DE'
. It gives me no error but the ordering also does not change (after makemigrations
and migrate
).
Maybe someone can guide me in the right direction. Thanks.