mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Test for customizable HyperlinkedModelSerializer url field name
This commit is contained in:
parent
e9b0d70f5d
commit
ed8143b2b0
|
@ -3,6 +3,7 @@ import json
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from rest_framework import generics, status, serializers
|
from rest_framework import generics, status, serializers
|
||||||
from rest_framework.compat import patterns, url
|
from rest_framework.compat import patterns, url
|
||||||
|
from rest_framework.settings import api_settings
|
||||||
from rest_framework.test import APIRequestFactory
|
from rest_framework.test import APIRequestFactory
|
||||||
from rest_framework.tests.models import (
|
from rest_framework.tests.models import (
|
||||||
Anchor, BasicModel, ManyToManyModel, BlogPost, BlogPostComment,
|
Anchor, BasicModel, ManyToManyModel, BlogPost, BlogPostComment,
|
||||||
|
@ -331,3 +332,48 @@ class TestOverriddenURLField(TestCase):
|
||||||
serializer.data,
|
serializer.data,
|
||||||
{'title': 'New blog post', 'url': 'foo bar'}
|
{'title': 'New blog post', 'url': 'foo bar'}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestURLFieldNameBySettings(TestCase):
|
||||||
|
urls = 'rest_framework.tests.test_hyperlinkedserializers'
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.saved_url_field_name = api_settings.URL_FIELD_NAME
|
||||||
|
api_settings.URL_FIELD_NAME = 'global_url_field'
|
||||||
|
|
||||||
|
class Serializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = BlogPost
|
||||||
|
fields = ('title', api_settings.URL_FIELD_NAME)
|
||||||
|
|
||||||
|
self.Serializer = Serializer
|
||||||
|
self.obj = BlogPost.objects.create(title="New blog post")
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
api_settings.URL_FIELD_NAME = self.saved_url_field_name
|
||||||
|
|
||||||
|
def test_overridden_url_field_name(self):
|
||||||
|
request = factory.get('/posts/')
|
||||||
|
serializer = self.Serializer(self.obj, context={'request': request})
|
||||||
|
self.assertIn(api_settings.URL_FIELD_NAME, serializer.data)
|
||||||
|
|
||||||
|
|
||||||
|
class TestURLFieldNameByOptions(TestCase):
|
||||||
|
urls = 'rest_framework.tests.test_hyperlinkedserializers'
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
class Serializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = BlogPost
|
||||||
|
fields = ('title', 'serializer_url_field')
|
||||||
|
url_field_name = 'serializer_url_field'
|
||||||
|
|
||||||
|
self.Serializer = Serializer
|
||||||
|
self.obj = BlogPost.objects.create(title="New blog post")
|
||||||
|
|
||||||
|
def test_overridden_url_field_name(self):
|
||||||
|
request = factory.get('/posts/')
|
||||||
|
serializer = self.Serializer(self.obj, context={'request': request})
|
||||||
|
self.assertIn(self.Serializer.Meta.url_field_name, serializer.data)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user