Select2 + Django: set preferred list items

324 Views Asked by At

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

1

There are 1 best solutions below

1
On BEST ANSWER

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

See this.