From d5e6284df36ec17fd0bec45648aa40354663a8cf Mon Sep 17 00:00:00 2001 From: Ben Graham Date: Wed, 23 Jan 2013 20:38:58 +1100 Subject: [PATCH] Failing test case for hyperlinked generic related fields. If the correct view_name is given to the ManyHyperlinkedRelatedField ('bookmarks-detail'), the test passes, though of course this only works because the test relationship is a Bookmark. When it does work, the returned URLs are not absolute ("http://testserver/..."). I assume this is because no real request/response context exists. --- rest_framework/tests/genericrelations.py | 31 +++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/rest_framework/tests/genericrelations.py b/rest_framework/tests/genericrelations.py index bc7378e12..e2c65a44c 100644 --- a/rest_framework/tests/genericrelations.py +++ b/rest_framework/tests/genericrelations.py @@ -1,9 +1,23 @@ from django.test import TestCase -from rest_framework import serializers +from rest_framework import serializers, generics from rest_framework.tests.models import * +from rest_framework.compat import patterns, url + + +class BookmarkDetail(generics.RetrieveAPIView): + model = ManyToManyModel + serializer_class = serializers.HyperlinkedModelSerializer + + +urlpatterns = patterns('', + url(r'^bookmark/(?P\d+)/$', BookmarkDetail.as_view(), name='bookmarks-detail'), +) class TestGenericRelations(TestCase): + + urls = 'rest_framework.tests.genericrelations' + def setUp(self): bookmark = Bookmark(url='https://www.djangoproject.com/') bookmark.save() @@ -31,3 +45,18 @@ class TestGenericRelations(TestCase): 'url': u'https://www.djangoproject.com/' } self.assertEquals(serializer.data, expected) + + def test_hyperlinked_generic_relation(self): + + class BookmarkSerializer(serializers.ModelSerializer): + tags = serializers.ManyHyperlinkedRelatedField(source='tags') + + class Meta: + model = Bookmark + exclude = ('id', 'url') + + serializer = BookmarkSerializer(self.bookmark) + expected = { + 'tags': [u'/bookmark/1/', u'/bookmark/2/'] + } + self.assertEquals(serializer.data, expected)