mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 11:33:59 +03:00
HyperlinkedModelSerializer lookup_field option should apply to HyperlinkedIdentityField
This commit is contained in:
parent
c36ff4e052
commit
d7753123f6
|
@ -493,8 +493,9 @@ class HyperlinkedIdentityField(Field):
|
||||||
self.view_name = kwargs.pop('view_name', None)
|
self.view_name = kwargs.pop('view_name', None)
|
||||||
# Optionally the format of the target hyperlink may be specified
|
# Optionally the format of the target hyperlink may be specified
|
||||||
self.format = kwargs.pop('format', None)
|
self.format = kwargs.pop('format', None)
|
||||||
|
lookup_field = kwargs.pop('lookup_field', None)
|
||||||
self.lookup_field = kwargs.pop('lookup_field', self.lookup_field)
|
if lookup_field is not None:
|
||||||
|
self.lookup_field = lookup_field
|
||||||
|
|
||||||
# These are pending deprecation
|
# These are pending deprecation
|
||||||
if 'pk_url_kwarg' in kwargs:
|
if 'pk_url_kwarg' in kwargs:
|
||||||
|
|
|
@ -907,6 +907,8 @@ class HyperlinkedModelSerializer(ModelSerializer):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(HyperlinkedModelSerializer, self).__init__(*args, **kwargs)
|
super(HyperlinkedModelSerializer, self).__init__(*args, **kwargs)
|
||||||
|
lookup_field = self.opts.lookup_field
|
||||||
|
self.fields['url'] = HyperlinkedIdentityField(lookup_field=lookup_field)
|
||||||
if self.opts.view_name is None:
|
if self.opts.view_name is None:
|
||||||
self.opts.view_name = self._get_default_view_name(self.opts.model)
|
self.opts.view_name = self._get_default_view_name(self.opts.model)
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,17 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
from django.db import models
|
||||||
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.response import Response
|
from rest_framework import serializers, viewsets
|
||||||
from rest_framework import viewsets
|
from rest_framework.compat import include, patterns, url
|
||||||
from rest_framework.decorators import link, action
|
from rest_framework.decorators import link, action
|
||||||
|
from rest_framework.response import Response
|
||||||
from rest_framework.routers import SimpleRouter
|
from rest_framework.routers import SimpleRouter
|
||||||
|
|
||||||
factory = RequestFactory()
|
factory = RequestFactory()
|
||||||
|
|
||||||
|
urlpatterns = patterns('',)
|
||||||
|
|
||||||
|
|
||||||
class BasicViewSet(viewsets.ViewSet):
|
class BasicViewSet(viewsets.ViewSet):
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
|
@ -49,3 +53,62 @@ class TestSimpleRouter(TestCase):
|
||||||
else:
|
else:
|
||||||
method_map = 'get'
|
method_map = 'get'
|
||||||
self.assertEqual(route.mapping[method_map], endpoint)
|
self.assertEqual(route.mapping[method_map], endpoint)
|
||||||
|
|
||||||
|
|
||||||
|
class RouterTestModel(models.Model):
|
||||||
|
uuid = models.CharField(max_length=20)
|
||||||
|
text = models.CharField(max_length=200)
|
||||||
|
|
||||||
|
|
||||||
|
class TestCustomLookupFields(TestCase):
|
||||||
|
"""
|
||||||
|
Ensure that custom lookup fields are correctly routed.
|
||||||
|
"""
|
||||||
|
urls = 'rest_framework.tests.test_routers'
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
class NoteSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = RouterTestModel
|
||||||
|
lookup_field = 'uuid'
|
||||||
|
fields = ('url', 'uuid', 'text')
|
||||||
|
|
||||||
|
class NoteViewSet(viewsets.ModelViewSet):
|
||||||
|
queryset = RouterTestModel.objects.all()
|
||||||
|
serializer_class = NoteSerializer
|
||||||
|
lookup_field = 'uuid'
|
||||||
|
|
||||||
|
RouterTestModel.objects.create(uuid='123', text='foo bar')
|
||||||
|
|
||||||
|
self.router = SimpleRouter()
|
||||||
|
self.router.register(r'notes', NoteViewSet)
|
||||||
|
|
||||||
|
from rest_framework.tests import test_routers
|
||||||
|
urls = getattr(test_routers, 'urlpatterns')
|
||||||
|
urls += patterns('',
|
||||||
|
url(r'^', include(self.router.urls)),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_custom_lookup_field_route(self):
|
||||||
|
detail_route = self.router.urls[-1]
|
||||||
|
detail_url_pattern = detail_route.regex.pattern
|
||||||
|
self.assertIn('<uuid>', detail_url_pattern)
|
||||||
|
|
||||||
|
def test_retrieve_lookup_field_list_view(self):
|
||||||
|
response = self.client.get('/notes/')
|
||||||
|
self.assertEquals(response.data,
|
||||||
|
[{
|
||||||
|
"url": "http://testserver/notes/123/",
|
||||||
|
"uuid": "123", "text": "foo bar"
|
||||||
|
}]
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_retrieve_lookup_field_detail_view(self):
|
||||||
|
response = self.client.get('/notes/123/')
|
||||||
|
self.assertEquals(response.data,
|
||||||
|
{
|
||||||
|
"url": "http://testserver/notes/123/",
|
||||||
|
"uuid": "123", "text": "foo bar"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user