This commit is contained in:
Scott White 2014-08-18 14:55:36 +00:00
commit d28c3f6025
3 changed files with 61 additions and 1 deletions

View File

@ -681,7 +681,15 @@ class ModelSerializer(Serializer):
forward_rels = [field for field in opts.fields if field.serialize]
forward_rels += [field for field in opts.many_to_many if field.serialize]
if hasattr(self.Meta, 'exclude'):
exclude_fields = self.Meta.exclude
else:
exclude_fields = []
for model_field in forward_rels:
if model_field.name in exclude_fields:
continue
has_through_model = False
if model_field.rel:

View File

@ -75,6 +75,16 @@ class ActionItem(RESTFrameworkModel):
info = CustomField(default='---', max_length=12)
# Models for testing #1604 regression
class UntouchablePerson(models.Model):
name = models.CharField(max_length=64)
class UntouchablePersonLog(models.Model):
person = models.ForeignKey(UntouchablePerson, editable=False)
date = models.DateField(null=False)
# Models for reverse relations
class Person(RESTFrameworkModel):
name = models.CharField(max_length=10)

View File

@ -10,7 +10,7 @@ from rest_framework import serializers, fields, relations
from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel, DefaultValueModel,
ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo, RESTFrameworkModel,
ForeignKeySource, ManyToManySource)
ForeignKeySource, ManyToManySource, UntouchablePerson, UntouchablePersonLog)
from rest_framework.tests.models import BasicModelSerializer
import datetime
import pickle
@ -98,6 +98,7 @@ class ActionItemSerializer(serializers.ModelSerializer):
class Meta:
model = ActionItem
class ActionItemSerializerOptionalFields(serializers.ModelSerializer):
"""
Intended to test that fields with `required=False` are excluded from validation.
@ -108,6 +109,20 @@ class ActionItemSerializerOptionalFields(serializers.ModelSerializer):
model = ActionItem
fields = ('title',)
class UntouchablePersonLogSerializerExcludedFields(serializers.ModelSerializer):
"""
Intended to test that fields with `editable=False` and that are included
in Meta.exclude are actually excluded from validation.
Tests regression for #1604
"""
class Meta:
model = UntouchablePersonLog
fields = ('date',)
exclude = ('person',)
class ActionItemSerializerCustomRestore(serializers.ModelSerializer):
class Meta:
@ -980,6 +995,33 @@ class WritableFieldDefaultValueTests(TestCase):
self.assertEqual(got, expected)
class ExcludedFieldTests(TestCase):
def setUp(self):
self.serializer_class = UntouchablePersonLogSerializerExcludedFields
# An UntouchablePerson instance to use for the relationship
self.person = UntouchablePerson(name='MC Hammer')
self.person.save()
# A log entry with non-editable relationship to the person
self.instance = UntouchablePersonLog(
person=self.person,
date=datetime.date(1990, 1, 13)
)
self.instance.save()
def test_excluded_noneditable_field_is_excluded(self):
# test for #1604
data = {
'date': datetime.date(1990, 1, 13)
}
serializer = UntouchablePersonLogSerializerExcludedFields(
instance=self.instance
)
self.assertEqual(serializer.data, data)
class RelatedFieldDefaultValueTests(WritableFieldDefaultValueTests):
def setUp(self):