mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
HyperlinkedModelSerializer with working HyperlinkedIdentityField, but no hyperlinked relations
This commit is contained in:
parent
ad2149767b
commit
d89d6887d2
|
@ -9,6 +9,7 @@ from django.conf import settings
|
||||||
from django.db import DEFAULT_DB_ALIAS
|
from django.db import DEFAULT_DB_ALIAS
|
||||||
from django.utils.encoding import is_protected_type, smart_unicode
|
from django.utils.encoding import is_protected_type, smart_unicode
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
from rest_framework.reverse import reverse
|
||||||
from rest_framework.compat import parse_date, parse_datetime
|
from rest_framework.compat import parse_date, parse_datetime
|
||||||
from rest_framework.compat import timezone
|
from rest_framework.compat import timezone
|
||||||
|
|
||||||
|
@ -173,6 +174,14 @@ class Field(object):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
class HyperlinkedIdentityField(Field):
|
||||||
|
def field_to_native(self, obj, field_name):
|
||||||
|
request = self.context.get('request', None)
|
||||||
|
view_name = self.parent.opts.view_name
|
||||||
|
view_kwargs = {'pk': obj.pk}
|
||||||
|
return reverse(view_name, kwargs=view_kwargs, request=request)
|
||||||
|
|
||||||
|
|
||||||
class RelatedField(Field):
|
class RelatedField(Field):
|
||||||
"""
|
"""
|
||||||
A base class for model related fields or related managers.
|
A base class for model related fields or related managers.
|
||||||
|
|
|
@ -396,3 +396,40 @@ class ModelSerializer(RelatedField, Serializer):
|
||||||
"""
|
"""
|
||||||
self.object.save()
|
self.object.save()
|
||||||
return self.object.object
|
return self.object.object
|
||||||
|
|
||||||
|
|
||||||
|
class HyperlinkedModelSerializerOptions(ModelSerializerOptions):
|
||||||
|
"""
|
||||||
|
Options for HyperlinkedModelSerializer
|
||||||
|
"""
|
||||||
|
def __init__(self, meta):
|
||||||
|
super(HyperlinkedModelSerializerOptions, self).__init__(meta)
|
||||||
|
self.view_name = getattr(meta, 'view_name', None)
|
||||||
|
|
||||||
|
|
||||||
|
class HyperlinkedModelSerializer(ModelSerializer):
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
_options_class = HyperlinkedModelSerializerOptions
|
||||||
|
_default_view_name = '%(model_name)s-detail'
|
||||||
|
|
||||||
|
url = HyperlinkedIdentityField()
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(HyperlinkedModelSerializer, self).__init__(*args, **kwargs)
|
||||||
|
if self.opts.view_name is None:
|
||||||
|
self.opts.view_name = self._get_default_view_name()
|
||||||
|
|
||||||
|
def _get_default_view_name(self):
|
||||||
|
"""
|
||||||
|
Return the view name to use if 'view_name' is not specified in 'Meta'
|
||||||
|
"""
|
||||||
|
model_meta = self.opts.model._meta
|
||||||
|
format_kwargs = {
|
||||||
|
'app_label': model_meta.app_label,
|
||||||
|
'model_name': model_meta.object_name.lower()
|
||||||
|
}
|
||||||
|
return self._default_view_name % format_kwargs
|
||||||
|
|
||||||
|
def get_pk_field(self, model_field):
|
||||||
|
return None
|
||||||
|
|
|
@ -46,7 +46,7 @@ DEFAULTS = {
|
||||||
|
|
||||||
'MODEL_SERIALIZER': 'rest_framework.serializers.ModelSerializer',
|
'MODEL_SERIALIZER': 'rest_framework.serializers.ModelSerializer',
|
||||||
'PAGINATION_SERIALIZER': 'rest_framework.pagination.PaginationSerializer',
|
'PAGINATION_SERIALIZER': 'rest_framework.pagination.PaginationSerializer',
|
||||||
'PAGINATE_BY': 20,
|
'PAGINATE_BY': None,
|
||||||
|
|
||||||
'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
|
'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
|
||||||
'UNAUTHENTICATED_TOKEN': None,
|
'UNAUTHENTICATED_TOKEN': None,
|
||||||
|
|
|
@ -13,7 +13,6 @@ class RootView(generics.ListCreateAPIView):
|
||||||
Example description for OPTIONS.
|
Example description for OPTIONS.
|
||||||
"""
|
"""
|
||||||
model = BasicModel
|
model = BasicModel
|
||||||
paginate_by = None
|
|
||||||
|
|
||||||
|
|
||||||
class InstanceView(generics.RetrieveUpdateDestroyAPIView):
|
class InstanceView(generics.RetrieveUpdateDestroyAPIView):
|
||||||
|
|
60
rest_framework/tests/hyperlinkedserializers.py
Normal file
60
rest_framework/tests/hyperlinkedserializers.py
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
from django.conf.urls 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 BasicModel
|
||||||
|
|
||||||
|
factory = RequestFactory()
|
||||||
|
|
||||||
|
|
||||||
|
class BasicList(generics.ListCreateAPIView):
|
||||||
|
model = BasicModel
|
||||||
|
model_serializer_class = serializers.HyperlinkedModelSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class BasicDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||||
|
model = BasicModel
|
||||||
|
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'),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestHyperlinkedView(TestCase):
|
||||||
|
urls = 'rest_framework.tests.hyperlinkedserializers'
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
"""
|
||||||
|
Create 3 BasicModel intances.
|
||||||
|
"""
|
||||||
|
items = ['foo', 'bar', 'baz']
|
||||||
|
for item in items:
|
||||||
|
BasicModel(text=item).save()
|
||||||
|
self.objects = BasicModel.objects
|
||||||
|
self.data = [
|
||||||
|
{'url': 'http://testserver/basic/%d/' % obj.id, 'text': obj.text}
|
||||||
|
for obj in self.objects.all()
|
||||||
|
]
|
||||||
|
self.list_view = BasicList.as_view()
|
||||||
|
self.detail_view = BasicDetail.as_view()
|
||||||
|
|
||||||
|
def test_get_list_view(self):
|
||||||
|
"""
|
||||||
|
GET requests to ListCreateAPIView should return list of objects.
|
||||||
|
"""
|
||||||
|
request = factory.get('/')
|
||||||
|
response = self.list_view(request).render()
|
||||||
|
self.assertEquals(response.status_code, status.HTTP_200_OK)
|
||||||
|
self.assertEquals(response.data, self.data)
|
||||||
|
|
||||||
|
def test_get_detail_view(self):
|
||||||
|
"""
|
||||||
|
GET requests to ListCreateAPIView should return list of objects.
|
||||||
|
"""
|
||||||
|
request = factory.get('/1')
|
||||||
|
response = self.detail_view(request, pk=1).render()
|
||||||
|
self.assertEquals(response.status_code, status.HTTP_200_OK)
|
||||||
|
self.assertEquals(response.data, self.data[0])
|
Loading…
Reference in New Issue
Block a user