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

Chosen url only accepts integers as id #72

Open
jaap3 opened this issue May 31, 2023 · 1 comment
Open

Chosen url only accepts integers as id #72

jaap3 opened this issue May 31, 2023 · 1 comment

Comments

@jaap3
Copy link

jaap3 commented May 31, 2023

Currently the urlpatterns generated by the ChooserViewSet only accept integers (d+) as the id parameter:

def get_urlpatterns(self):
return super().get_urlpatterns() + [
re_path(r'^$', self.choose_view, name='choose'),
re_path(r'^(\d+)/$', self.chosen_view, name='chosen'),
re_path(r'^chosen-multiple/$', self.chosen_multiple_view, name='chosen_multiple'),
]

This makes it impossible to create a chooser that has UUIDs as a primary key.

@mojeto
Copy link

mojeto commented Jun 28, 2023

Minimum code to get around this:

from django.urls import path
from generic_chooser.views import BaseChosenView, DRFChooserViewSet

class UUIDCapableChosenView(BaseChosenView):
    def get(self, request, pk):
        # needed to prevent TypeError: expected string or bytes-like object, got 'UUID'
        return super().get(request, str(pk))


class ChooserViewSet(DRFChooserViewSet):
    base_chosen_view_class = UUIDCapableChosenView
   
    def get_urlpatterns(self):
        urls = super().get_urlpatterns()
        urls[1] = path("<uuid:pk>/", self.chosen_view, name="chosen")
        return urls

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

No branches or pull requests

2 participants