3

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.

Ryan113
  • 676
  • 1
  • 10
  • 27

1 Answers1

6

You must give a unique name for the Named capturing group.

url(r'^video/(?P<pk1>[0-9]+)/quiz/(?P<pk2>[0-9]+)/list/$',
    views.QuizList.as_view(),
    name='quizzes-list'),

Access the corresponding pk* values in the view through,

pk1 = kwargs.get('pk1', None)
pk2 = kwargs.get('pk2', None)

ex:

# quiz
class QuizList(generics.ListCreateAPIView):
    queryset = Quizzes.objects.all()
    serializer_class = QuizSerializer

    def get(self, *args, **kwargs):
        pk1 = kwargs.get('pk1', None)
        pk2 = kwargs.get('pk2', None)
        print pk1
        print pk2
        return super(QuizList, self).get(*args, **kwargs)
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • I'm still not understanding. I'm new to this. I posted my views. How would I implement the corresponding pk* to them? – Ryan113 Mar 24 '16 at 05:44
  • I updated everything, but am now getting a new error which I posted above. I'm confused about the error because 'quizzes-list' is listed in the url name and I have no lookup_field. Any direction? Appreciate all the help. – Ryan113 Mar 24 '16 at 21:45
  • 1
    @Ryan113 take a look at http://stackoverflow.com/questions/20550598/django-rest-framework-could-not-resolve-url-for-hyperlinked-relationship-using – Avinash Raj Mar 26 '16 at 02:38