From 1400cfa4bebf65769fcbf956d02e65f9fc4aa589 Mon Sep 17 00:00:00 2001 From: rafaelhenter Date: Mon, 31 Aug 2020 14:45:36 -0300 Subject: [PATCH 1/2] BugFix when ID is a UUID --- smart_selects/encoders.py | 9 +++++++++ smart_selects/widgets.py | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 smart_selects/encoders.py diff --git a/smart_selects/encoders.py b/smart_selects/encoders.py new file mode 100644 index 0000000..bec91f1 --- /dev/null +++ b/smart_selects/encoders.py @@ -0,0 +1,9 @@ +import json +from uuid import UUID + + +class UUIDEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, UUID): + return obj.hex + return json.JSONEncoder.default(self, obj) diff --git a/smart_selects/widgets.py b/smart_selects/widgets.py index d0c06af..8133e55 100644 --- a/smart_selects/widgets.py +++ b/smart_selects/widgets.py @@ -4,6 +4,9 @@ from django.apps import apps from django.conf import settings + +from encoders import UUIDEncoder + try: from django.core.urlresolvers import reverse except ImportError: @@ -253,7 +256,7 @@ def render(self, name, value, attrs=None, choices=(), renderer=None): attrs["data-chainfield"] = chain_field attrs["data-url"] = url - attrs["data-value"] = "null" if value is None else json.dumps(value) + attrs["data-value"] = "null" if value is None else json.dumps(value, cls=UUIDEncoder) attrs["data-auto_choose"] = auto_choose attrs["name"] = name final_attrs = self.build_attrs(attrs) From fed0c82f76bc085dd129f53ac0fff97b2e94b649 Mon Sep 17 00:00:00 2001 From: rafaelhenter Date: Mon, 31 Aug 2020 14:51:22 -0300 Subject: [PATCH 2/2] fix auto import --- smart_selects/widgets.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/smart_selects/widgets.py b/smart_selects/widgets.py index 8133e55..a16788e 100644 --- a/smart_selects/widgets.py +++ b/smart_selects/widgets.py @@ -5,8 +5,6 @@ from django.apps import apps from django.conf import settings -from encoders import UUIDEncoder - try: from django.core.urlresolvers import reverse except ImportError: @@ -17,6 +15,7 @@ from django.utils.encoding import force_text from django.utils.html import escape +from smart_selects.encoders import UUIDEncoder from smart_selects.utils import unicode_sorter, sort_results get_model = apps.get_model