djangodjango-modelsdjango-filterdjango-countries

How to use the verbose name of a language choice in Django-Countries with Django Filter


I have this Django Filter:

from django_countries.data import COUNTRIES

owner__nationality = filters.MultipleChoiceFilter(choices=COUNTRIES, widget=Select2MultipleWidget)

So I guessed I just use the original choices field to filter on nationality (for which I used Django Countries to fill the data) As you can see in the source code here, the import is correct: https://github.com/SmileyChris/django-countries/blob/master/django_countries/data.py

However on the front end the dropdown looks like this:

enter image description here

How can I get full countries to be displayed there? I also don't quite understand why there is only one letter there. Can somebody clarify?

By the way I know about get_FOO_display()


Solution

  • MultipleChoiceFilter takes iterable of tuples as as choices. Package you mentioned provides COUNTRIES as dictionary. Try doing

    from django_countries.data import COUNTRIES
    
    owner__nationality = filters.MultipleChoiceFilter(
        choices=[(k, v) for k, v in COUNTRIES.items()],
        widget=Select2MultipleWidget
    )