I am currently trying to build an API using the Django Rest Framework. Currently I want to be able to have multiple pks in a single url, but when I try I get an error:
django.core.exceptions.ImproperlyConfigured: "^video/(?P[0-9]+)/quiz/(?P[0-9]+)/list/$" is not a valid regular expression: redefinition of group name 'pk' as group 2; was group 1 at position 31
Here are my URLS:
url(r'^video/(?P<pk>[0-9]+)/quiz/(?P<pk>[0-9]+)/list/$',
views.QuizList.as_view(),
name='quizzes-list'),
url(r'^video/(?P<pk>[0-9]+)/quiz/(?P<pk>[0-9]+)/detail/$',
views.QuizDetail.as_view(),
name='quizzes-detail'),
I am going to have multiple quizzes for a video and want to be able to reach them through a certain pk in my url.
Is there a simple method to do this or am missing the proper way to do this?
Thank you
Edit* Here are my views:
class VideoList(generics.ListCreateAPIView):
queryset = Video.objects.all()
serializer_class = VideoSerializer
class VideoDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Video.objects.all()
serializer_class = VideoSerializer
# quiz
class QuizList(generics.ListCreateAPIView):
queryset = Quizzes.objects.all()
serializer_class = QuizSerializer
class QuizDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Quizzes.objects.all()
serializer_class = QuizSerializer
Updated urls:
###
# Quiz urls
###
url(r'^video/(?P<pk1>[0-9]+)/quiz/(?P<pk2>[0-9]+)/list/$',
views.QuizList.as_view(),
name='quizzes-list'),
url(r'^video/(?P<pk1>[0-9]+)/quiz/(?P<pk2>[0-9]+)/detail/$',
views.QuizDetail.as_view(),
name='quizzes-detail'),
Updated views:
class QuizList(generics.ListCreateAPIView):
queryset = Quizzes.objects.all()
serializer_class = QuizSerializer
def get(self, request, *args, **kwargs):
pk1 = kwargs.get('pk1', None)
pk2 = kwargs.get('pk2', None)
print(pk1)
print(pk2)
return self.list(request, *args, **kwargs)
New Error:
Could not resolve URL for hyperlinked relationship using view name "quizzes-list". You may have failed to include the related model in your API, or incorrectly configured the
lookup_field
attribute on this field.