pythondjangojquery-select2django-countries

Select2 + Django: set preferred list items


I use select2]1 with django-countries in order to give the user a dropdown of possible countries. I now would like to either (1) split the dropdown or (2) rearrange the order of the items/countries, i.e.:

Currently I get an alphabetically ordered list:

- select country
  - Afghanistan
  - Albania
  - Algeria
  ...
  - Kenya
  ...
  - Zimbabwe

What I would like is: (1)

- select country
  - Germany
  - Austria
  - Switzerland
  ---------------------------
  - Afghanistan
  - Albania
  - Algeria
  ...
  - Kenya
  ...
  - Zimbabwe

or (2)

- select country
  - Germany
  - Austria
  - Switzerland
  - Afghanistan
  - Albania
  - Algeria
  ...
  - Kenya
  ...
  - Zimbabwe

This is my js:

$(document).ready(function () {
    $("#id_nationality").select2({
        default: 'Germany',
        placeholder: 'select country'
    });
});

I use crispy-forms for form rendering


Solution

  • Solved it, as django-countries has this function included.

    See this.