Repository: SmileyChris/django-countries

Issue:
Duplicates should be stripped before saving a from a CountryField which has multiple=True
I'm using `CountrySelectWidget` in the admin interface, along with the following settings:
```python
COUNTRIES_FIRST = ['IE']
COUNTRIES_FIRST_REPEAT = True
```
This particular scenario leads to some unexpected behavior: When I select Ireland from _either_ the top section of the widget _or_ from the regular list and save the object, the model field will contain `[Country(code='IE')]` (as expected). When I then reopen the objects' change view, the widget will have _both_ Irelands selected and store `[Country(code='IE'), Country(code='IE')]` on a subsequent save.

From my point of view, rendering both Irelands as selected in the widget is acceptable since they point to the same country, but duplicating countries in the database is not. I suggest to strip the list of countries to be saved from duplicates.

Discussion:

Patch:
diff --git a/django_countries/fields.py b/django_countries/fields.py
index aabd8d1..36a8c4a 100644
--- a/django_countries/fields.py
+++ b/django_countries/fields.py
@@ -1,4 +1,5 @@
 import re
+from collections import OrderedDict
 from typing import Any, Iterable, Optional, Tuple, Type, Union, cast
 from urllib import parse as urlparse
 
@@ -353,6 +354,7 @@ class CountryField(CharField):
                 iter(value)
             except TypeError:
                 value = [value]
+        value = list(OrderedDict.fromkeys(value))  # remove duplicates while maintaining order
         return list(filter(None, [self.country_to_text(c) for c in value]))
 
     def deconstruct(self):


Test patch:
diff --git a/django_countries/tests/test_fields.py b/django_countries/tests/test_fields.py
index b2c60d5..877eb88 100644
--- a/django_countries/tests/test_fields.py
+++ b/django_countries/tests/test_fields.py
@@ -422,6 +422,15 @@ class TestCountryMultiple(TestCase):
         self.assertEqual(obj.countries[0], "AU")
         self.assertEqual(obj.countries[1], "NZ")
 
+    def test_multiple_with_duplicates(self):
+        obj = MultiCountry(countries="AU,NZ,AU")
+        for country in obj.countries:
+            self.assertTrue(isinstance(country, fields.Country))
+        self.assertEqual(obj.countries, ["AU", "NZ"])
+
+        obj = MultiCountry(countries="")
+        self.assertEqual(obj.countries, [])
+
     def test_set_text(self):
         obj = MultiCountry()
         obj.countries = "NZ,AU"