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

Add custom JSON encoder #159

Closed
N1K1TAS95 opened this issue Oct 23, 2022 · 3 comments
Closed

Add custom JSON encoder #159

N1K1TAS95 opened this issue Oct 23, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@N1K1TAS95
Copy link

Hi there!
I was wondering if it's possibile to add some kind of option to specify a custom JSON encoder in AutoResponseView view.
I'm using this package https://github.com/nshafer/django-hashid-field and if I don't specify the specific JSON encoder while I'm using ModelSelect2Widget widget, it will throw an Object of type Hashid is not JSON serializable error.

Here the AutoResponseView that works:

from django.core import signing
from django.core.signing import BadSignature
from django.core.serializers.json import DjangoJSONEncoder
from django.http import Http404, JsonResponse
from django.views.generic.list import BaseListView
from hashid_field.hashid import Hashid # from the package [https://github.com/nshafer/django-hashid-field](https://github.com/nshafer/django-hashid-field)

from .cache import cache
from .conf import settings

# custom JSON encoder
class HashidJSONEncoder(DjangoJSONEncoder):

    def default(self, o):
        if isinstance(o, Hashid):
            return str(o)
        return super().default(o)


class AutoResponseView(BaseListView):

    def get(self, request, *args, **kwargs):
        self.widget = self.get_widget_or_404()
        self.term = kwargs.get("term", request.GET.get("term", ""))
        self.object_list = self.get_queryset()
        context = self.get_context_data()
        return JsonResponse(
            {
                "results": [
                    {"text": self.widget.label_from_instance(obj), "id": obj.pk}
                    for obj in context["object_list"]
                ],
                "more": context["page_obj"].has_next(),
            },
            encoder=HashidJSONEncoder # using custom JSON encoder
        )

Thanks a lot!

@codingjoe codingjoe added the enhancement New feature or request label Oct 28, 2022
@codingjoe
Copy link
Owner

@N1K1TAS95 thanks for reaching out and suggesting the improvement. I am usually not a big fan of adding too many settings. But I believe in this case a custom JSON encoder can be rather helpful. Would you care to contribute this feature yourself? Cheers, Joe

@N1K1TAS95
Copy link
Author

@codingjoe I'll try. 😉

@N1K1TAS95
Copy link
Author

Hi there!
@codingjoe, I've added a new PR #160.
I guess the documentation needs to be updated, if the PR it's ok, I'll try to update later.
BR, Nikita

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants