This commit is contained in:
Marko Tibold 2012-11-10 12:23:19 +01:00
parent df689a7442
commit 2a2ce406bc
2 changed files with 7 additions and 4 deletions

View File

@ -522,7 +522,10 @@ class HyperlinkedRelatedField(RelatedField):
view_name = self.view_name
request = self.context.get('request', None)
format = self.format or self.context.get('format', None)
kwargs = {self.pk_url_kwarg: obj.pk}
pk = getattr(obj, 'pk', None)
if pk is None:
return
kwargs = {self.pk_url_kwarg: pk}
try:
return reverse(view_name, kwargs=kwargs, request=request, format=format)
except:

View File

@ -224,11 +224,12 @@ class TestOptionalRelationHyperlinkedView(TestCase):
def setUp(self):
"""
Create 1 OptionaRelationModel intances.
Create 1 OptionalRelationModel intances.
"""
OptionalRelationModel().save()
self.objects = OptionalRelationModel.objects
self.detail_view = OptionalRelationDetail.as_view()
self.data = {"url": "http://testserver/optionalrelation/1/", "other": None}
def test_get_detail_view(self):
"""
@ -238,5 +239,4 @@ class TestOptionalRelationHyperlinkedView(TestCase):
request = factory.get('/optionalrelationmodel-detail/1')
response = self.detail_view(request, pk=1).render()
self.assertEquals(response.status_code, status.HTTP_200_OK)
self.assertEquals(response.data, self.data)