Translated model SelectField not sorted in the correct language
See original GitHub issueHi, I have a Select with objects that are countries, so model is called Country with a “name” and “code” attributes. Names are perfectly translated but their order in a select box rendered from a model form, sorted alphabetically in english (default language), whatever I select in languages (except english of course)…
Code from models.py :
class Country(models.Model):
code = models.CharField(max_length=4, unique=True)
name = models.CharField(max_length=255)
def __str__(self):
return (self.name)
class Meta:
ordering = ['name']
Code from translations.py
class CountryTranslationOptions(TranslationOptions):
fields = ('name',)
translator.register(Country, CountryTranslationOptions)
Code from forms.py
class CountriesSelectForm(forms.Form):
country = forms.ModelChoiceField(queryset=Country.objects.all().order_by("name"),
widget=forms.Select(),
label=_('Country'),
required=False)
)
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
django-multilingual: order drop down field by translated field
You could try using a custom widget in the form so that the sorting happens right before django would do the translation. Maybe...
Read more >Translated fields are still sorted using English Sort Order
When sorting on a translated field, the results are being sorted based on the English text, not the translation. Steps to Reproduce Pre-requisite:...
Read more >Rules for Translated Field Access - django-modeltranslation
In this case, if title is missing in active language and any of fallback languages, news title will be '-- sorry, no translation...
Read more >Order of translated items in a drop down select field - Toolset
Hi Support We have a custom field COUNTRY which has a list of 36 countries as options for a drop down field. The...
Read more >Creating RTF Templates Using the Template Builder for Word
The sample data from the data model is loaded to the Template Builder. ... using the template in a language that prints the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Amazing, thanks for sharing your solution @EParisot!
@antoiloui in fact I just came accross it today, and fixed it by sorting the select options on the client side. See following js code (usage: sortSelect(document.getElementById(“select_id”))😉