0

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()

JPG
  • 82,442
  • 19
  • 127
  • 206
Raf Rasenberg
  • 534
  • 2
  • 14
  • 27
  • you just need to add in models.py **` Country = CountryField(blank_label='Select Country') `** why you want select multiple country – Sangram Apr 26 '19 at 06:03
  • I try to pull the hard-coded choice data, so I can use it with `Django-Filter` Not the `CountryField`. – Raf Rasenberg Apr 26 '19 at 06:08
  • The models are already declared and populated with the `CountryField`. Right now I try to get the choice data so I can run it against a Queryset. – Raf Rasenberg Apr 26 '19 at 06:10

1 Answers1

2

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
)
Nafees Anwar
  • 6,324
  • 2
  • 23
  • 42