mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Reproduces #380
This commit is contained in:
parent
da1aa5542c
commit
df689a7442
|
@ -2,7 +2,7 @@ from django.conf.urls.defaults import patterns, url
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.test.client import RequestFactory
|
from django.test.client import RequestFactory
|
||||||
from rest_framework import generics, status, serializers
|
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()
|
factory = RequestFactory()
|
||||||
|
|
||||||
|
@ -67,6 +67,11 @@ class AlbumDetail(generics.RetrieveAPIView):
|
||||||
model = Album
|
model = Album
|
||||||
|
|
||||||
|
|
||||||
|
class OptionalRelationDetail(generics.RetrieveAPIView):
|
||||||
|
model = OptionalRelationModel
|
||||||
|
model_serializer_class = serializers.HyperlinkedModelSerializer
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
url(r'^basic/$', BasicList.as_view(), name='basicmodel-list'),
|
url(r'^basic/$', BasicList.as_view(), name='basicmodel-list'),
|
||||||
url(r'^basic/(?P<pk>\d+)/$', BasicDetail.as_view(), name='basicmodel-detail'),
|
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'^posts/(?P<pk>\d+)/$', BlogPostDetail.as_view(), name='blogpost-detail'),
|
||||||
url(r'^comments/$', BlogPostCommentListCreate.as_view(), name='blogpostcomment-list'),
|
url(r'^comments/$', BlogPostCommentListCreate.as_view(), name='blogpostcomment-list'),
|
||||||
url(r'^albums/(?P<title>\w[\w-]*)/$', AlbumDetail.as_view(), name='album-detail'),
|
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(response.status_code, status.HTTP_201_CREATED)
|
||||||
self.assertEqual(self.post.photo_set.count(), 1)
|
self.assertEqual(self.post.photo_set.count(), 1)
|
||||||
self.assertEqual(self.post.photo_set.all()[0].description, 'A test photo')
|
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 OptionaRelationModel intances.
|
||||||
|
"""
|
||||||
|
OptionalRelationModel().save()
|
||||||
|
self.objects = OptionalRelationModel.objects
|
||||||
|
self.detail_view = OptionalRelationDetail.as_view()
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -149,3 +149,8 @@ class Person(RESTFrameworkModel):
|
||||||
# Model for issue #324
|
# Model for issue #324
|
||||||
class BlankFieldModel(RESTFrameworkModel):
|
class BlankFieldModel(RESTFrameworkModel):
|
||||||
title = models.CharField(max_length=100, blank=True)
|
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