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..a16788e 100644 --- a/smart_selects/widgets.py +++ b/smart_selects/widgets.py @@ -4,6 +4,7 @@ from django.apps import apps from django.conf import settings + try: from django.core.urlresolvers import reverse except ImportError: @@ -14,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 @@ -253,7 +255,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)