mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
added testcase for custom slug field in hyperlinkedrelatedfield
This commit is contained in:
parent
44449fa1f5
commit
03095f607a
|
@ -493,6 +493,12 @@ class HyperlinkedRelatedField(RelatedField):
|
||||||
self.format = kwargs.pop('format', None)
|
self.format = kwargs.pop('format', None)
|
||||||
super(HyperlinkedRelatedField, self).__init__(*args, **kwargs)
|
super(HyperlinkedRelatedField, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def get_slug_field(self):
|
||||||
|
"""
|
||||||
|
Get the name of a slug field to be used to look up by slug.
|
||||||
|
"""
|
||||||
|
return self.slug_field
|
||||||
|
|
||||||
def to_native(self, obj):
|
def to_native(self, obj):
|
||||||
view_name = self.view_name
|
view_name = self.view_name
|
||||||
request = self.context.get('request', None)
|
request = self.context.get('request', None)
|
||||||
|
|
|
@ -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
|
from rest_framework.tests.models import Anchor, BasicModel, ManyToManyModel, BlogPost, BlogPostComment, Album, Photo
|
||||||
|
|
||||||
factory = RequestFactory()
|
factory = RequestFactory()
|
||||||
|
|
||||||
|
@ -15,6 +15,14 @@ class BlogPostCommentSerializer(serializers.Serializer):
|
||||||
return BlogPostComment(**attrs)
|
return BlogPostComment(**attrs)
|
||||||
|
|
||||||
|
|
||||||
|
class PhotoSerializer(serializers.Serializer):
|
||||||
|
description = serializers.CharField()
|
||||||
|
album_url = serializers.HyperlinkedRelatedField(source='album', view_name='album-detail', queryset=Album.objects.all(), slug_field='title', slug_url_kwargs='title')
|
||||||
|
|
||||||
|
def restore_object(self, attrs, instance=None):
|
||||||
|
return Photo(**attrs)
|
||||||
|
|
||||||
|
|
||||||
class BasicList(generics.ListCreateAPIView):
|
class BasicList(generics.ListCreateAPIView):
|
||||||
model = BasicModel
|
model = BasicModel
|
||||||
model_serializer_class = serializers.HyperlinkedModelSerializer
|
model_serializer_class = serializers.HyperlinkedModelSerializer
|
||||||
|
@ -48,6 +56,16 @@ class BlogPostCommentListCreate(generics.ListCreateAPIView):
|
||||||
class BlogPostDetail(generics.RetrieveAPIView):
|
class BlogPostDetail(generics.RetrieveAPIView):
|
||||||
model = BlogPost
|
model = BlogPost
|
||||||
|
|
||||||
|
|
||||||
|
class PhotoListCreate(generics.ListCreateAPIView):
|
||||||
|
model = Photo
|
||||||
|
model_serializer_class = PhotoSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class AlbumDetail(generics.RetrieveAPIView):
|
||||||
|
model = Album
|
||||||
|
|
||||||
|
|
||||||
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'),
|
||||||
|
@ -55,7 +73,9 @@ urlpatterns = patterns('',
|
||||||
url(r'^manytomany/$', ManyToManyList.as_view(), name='manytomanymodel-list'),
|
url(r'^manytomany/$', ManyToManyList.as_view(), name='manytomanymodel-list'),
|
||||||
url(r'^manytomany/(?P<pk>\d+)/$', ManyToManyDetail.as_view(), name='manytomanymodel-detail'),
|
url(r'^manytomany/(?P<pk>\d+)/$', ManyToManyDetail.as_view(), name='manytomanymodel-detail'),
|
||||||
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'^photos/$', PhotoListCreate.as_view(), name='photo-list')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -166,3 +186,27 @@ class TestCreateWithForeignKeys(TestCase):
|
||||||
self.assertEqual(response.status_code, 201)
|
self.assertEqual(response.status_code, 201)
|
||||||
self.assertEqual(self.post.blogpostcomment_set.count(), 1)
|
self.assertEqual(self.post.blogpostcomment_set.count(), 1)
|
||||||
self.assertEqual(self.post.blogpostcomment_set.all()[0].text, 'A test comment')
|
self.assertEqual(self.post.blogpostcomment_set.all()[0].text, 'A test comment')
|
||||||
|
|
||||||
|
|
||||||
|
class TestCreateWithForeignKeysAndCustomSlug(TestCase):
|
||||||
|
urls = 'rest_framework.tests.hyperlinkedserializers'
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
"""
|
||||||
|
Create an Album
|
||||||
|
"""
|
||||||
|
self.post = Album.objects.create(title='test-album')
|
||||||
|
self.list_create_view = PhotoListCreate.as_view()
|
||||||
|
|
||||||
|
def test_create_photo(self):
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'description': 'A test photo',
|
||||||
|
'album_url': 'http://testserver/albums/test-album/'
|
||||||
|
}
|
||||||
|
|
||||||
|
request = factory.post('/photos/', data=data)
|
||||||
|
response = self.list_create_view(request).render()
|
||||||
|
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')
|
||||||
|
|
|
@ -118,6 +118,15 @@ class BlogPostComment(RESTFrameworkModel):
|
||||||
blog_post = models.ForeignKey(BlogPost)
|
blog_post = models.ForeignKey(BlogPost)
|
||||||
|
|
||||||
|
|
||||||
|
class Album(RESTFrameworkModel):
|
||||||
|
title = models.CharField(max_length=100, unique=True)
|
||||||
|
|
||||||
|
|
||||||
|
class Photo(RESTFrameworkModel):
|
||||||
|
description = models.TextField()
|
||||||
|
album = models.ForeignKey(Album)
|
||||||
|
|
||||||
|
|
||||||
class Person(RESTFrameworkModel):
|
class Person(RESTFrameworkModel):
|
||||||
name = models.CharField(max_length=10)
|
name = models.CharField(max_length=10)
|
||||||
age = models.IntegerField(null=True, blank=True)
|
age = models.IntegerField(null=True, blank=True)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user