Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve #159 -- Add custom JSONEncoder support via setting #160

Merged
merged 3 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions django_select2/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ class Select2Conf(AppConf):
``settings.DJANGO_SELECT2_I18N`` refers to :attr:`.I18N_PATH`.
"""

JSON_ENCODER = 'django.core.serializers.json.DjangoJSONEncoder'
"""
A :class:`JSONEncoder<json.JSONEncoder>` used to generate the API response for the model widgets.

A custom JSON encoder might be useful when your models uses
a special primary key, that isn't serializable by the default encoder.
"""

class Meta:
"""Prefix for all Django-Select2 settings."""

Expand Down
4 changes: 3 additions & 1 deletion django_select2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.core.signing import BadSignature
from django.http import Http404, JsonResponse
from django.views.generic.list import BaseListView
from django.utils.module_loading import import_string

from .cache import cache
from .conf import settings
Expand Down Expand Up @@ -43,7 +44,8 @@ def get(self, request, *args, **kwargs):
for obj in context["object_list"]
],
"more": context["page_obj"].has_next(),
}
},
encoder=import_string(settings.SELECT2_JSON_ENCODER)
)

def get_queryset(self):
Expand Down