I just got my hypermedia setup working and realized that my child models seem to be generating incorrect one-to-many inverse urls
For example
In the database I have a session with a speaker. The session pk is #2 and the speaker associated has a pk of #3
When I hit the root sessions list url I get the following (all correct so far)
[
{
"id": 2,
"url": "http://localhost:8000/codecamp/sessions/2/",
"name": "Getting started with ember.js",
"room": "Room B",
"desc": "about ember .. huh",
"speakers": "http://localhost:8000/codecamp/sessions/2/speakers/"
}
]
Now when I click the speakers link I get the following (not correct)
[
{
"id": 3,
"url": "http://localhost:8000/codecamp/speakers/3/",
"name": "Joel Taddei",
"session": "http://localhost:8000/codecamp/sessions/3/"
}
]
What is wrong here is that session (the url just above) should link back to session with pk #2 (not #3)
Here is my urls.py
url(r'^codecamp/speakers/$', csrf_exempt(SpeakerDetail.as_view()), name='speaker-list'),
url(r'^codecamp/sessions/(?P<pk>[0-9]+)/speakers/$', csrf_exempt(SessionSpeakers.as_view()), name='session-speakers'),
url(r'^codecamp/sessions/(?P<pk>\d+)/$', csrf_exempt(SessionDetail.as_view()), name='session-detail')
Here are the 3 views above
class SessionDetail(generics.RetrieveUpdateDestroyAPIView):
model = Session
serializer_class = serializers.SessionSerializer
class SessionSpeakers(generics.ListCreateAPIView):
model = Speaker
serializer_class = serializers.SpeakerSerializer
def get_queryset(self):
session_pk = self.kwargs.get('pk', None)
if session_pk is not None:
return Speaker.objects.filter(session__pk=session_pk)
return []
class SpeakerDetail(generics.RetrieveUpdateDestroyAPIView):
model = Speaker
serializer_class = serializers.SpeakerSerializer
Here is the serializers
class SessionSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.IntegerField()
url = serializers.HyperlinkedIdentityField(view_name='session-detail')
speakers = serializers.HyperlinkedIdentityField(view_name='session-speakers')
class Meta:
model = Session
fields = ('id', 'url', 'name', 'room', 'desc', 'speakers')
class SpeakerSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.IntegerField()
url = serializers.HyperlinkedIdentityField(view_name='speaker-detail')
session = serializers.HyperlinkedIdentityField(view_name='session-detail') #this seems to be the broken code using pk of speaker?
class Meta:
model = Speaker
fields = ('id', 'url', 'name', 'session')
My first question is - am I doing something wrong in the serializer that is causing this to gen the wrong id?
If not -is this a legit bug in the framework?
Thank you in advance