question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Translated model SelectField not sorted in the correct language

See original GitHub issue

Hi, 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:open
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
antoilouicommented, Apr 11, 2022

Amazing, thanks for sharing your solution @EParisot!

1reaction
EParisotcommented, Apr 10, 2022

@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”))😉

    function sortSelect(selElem) {
		var selected = selElem.options[selElem.selectedIndex].value;
		var tmpAry = new Array();
		for (var i=0;i<selElem.options.length;i++) {
			tmpAry[i] = new Array();
			tmpAry[i][0] = selElem.options[i].text;
			tmpAry[i][1] = selElem.options[i].value;
		}
		tmpAry.sort();
		if (selElem.options.length > 0) {
			selElem.options[0] = null;
		}
		for (var i=0;i<tmpAry.length;i++) {
			var op = new Option(tmpAry[i][0], tmpAry[i][1]);
			selElem.options[i] = op;
			if (tmpAry[i][1] == selected) {
				selElem.selectedIndex = i;
			}
		}
		return;
	}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found