mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
commit
02c1b5988b
|
@ -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:
|
||||
|
|
|
@ -2,7 +2,7 @@ from django.conf.urls.defaults import patterns, url
|
|||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
from rest_framework import generics, status, serializers
|
||||
from rest_framework.tests.models import Anchor, BasicModel, ManyToManyModel, BlogPost, BlogPostComment, Album, Photo
|
||||
from rest_framework.tests.models import Anchor, BasicModel, ManyToManyModel, BlogPost, BlogPostComment, Album, Photo, OptionalRelationModel
|
||||
|
||||
factory = RequestFactory()
|
||||
|
||||
|
@ -67,6 +67,11 @@ class AlbumDetail(generics.RetrieveAPIView):
|
|||
model = Album
|
||||
|
||||
|
||||
class OptionalRelationDetail(generics.RetrieveAPIView):
|
||||
model = OptionalRelationModel
|
||||
model_serializer_class = serializers.HyperlinkedModelSerializer
|
||||
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^basic/$', BasicList.as_view(), name='basicmodel-list'),
|
||||
url(r'^basic/(?P<pk>\d+)/$', BasicDetail.as_view(), name='basicmodel-detail'),
|
||||
|
@ -76,7 +81,8 @@ urlpatterns = patterns('',
|
|||
url(r'^posts/(?P<pk>\d+)/$', BlogPostDetail.as_view(), name='blogpost-detail'),
|
||||
url(r'^comments/$', BlogPostCommentListCreate.as_view(), name='blogpostcomment-list'),
|
||||
url(r'^albums/(?P<title>\w[\w-]*)/$', AlbumDetail.as_view(), name='album-detail'),
|
||||
url(r'^photos/$', PhotoListCreate.as_view(), name='photo-list')
|
||||
url(r'^photos/$', PhotoListCreate.as_view(), name='photo-list'),
|
||||
url(r'^optionalrelation/(?P<pk>\d+)/$', OptionalRelationDetail.as_view(), name='optionalrelationmodel-detail'),
|
||||
)
|
||||
|
||||
|
||||
|
@ -211,3 +217,26 @@ class TestCreateWithForeignKeysAndCustomSlug(TestCase):
|
|||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(self.post.photo_set.count(), 1)
|
||||
self.assertEqual(self.post.photo_set.all()[0].description, 'A test photo')
|
||||
|
||||
|
||||
class TestOptionalRelationHyperlinkedView(TestCase):
|
||||
urls = 'rest_framework.tests.hyperlinkedserializers'
|
||||
|
||||
def setUp(self):
|
||||
"""
|
||||
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):
|
||||
"""
|
||||
GET requests to RetrieveAPIView with optional relations should return None
|
||||
for non existing relations.
|
||||
"""
|
||||
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)
|
||||
|
|
|
@ -149,3 +149,8 @@ class Person(RESTFrameworkModel):
|
|||
# Model for issue #324
|
||||
class BlankFieldModel(RESTFrameworkModel):
|
||||
title = models.CharField(max_length=100, blank=True)
|
||||
|
||||
|
||||
# Model for issue #380
|
||||
class OptionalRelationModel(RESTFrameworkModel):
|
||||
other = models.ForeignKey('OptionalRelationModel', blank=True, null=True)
|
||||
|
|
Loading…
Reference in New Issue
Block a user