mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
updates test model definition due to RelationField changes
This commit is contained in:
parent
5d148207bb
commit
5edf19e919
|
@ -182,7 +182,7 @@ class RelatedField(WritableField):
|
||||||
if value in self.null_values:
|
if value in self.null_values:
|
||||||
if self.required:
|
if self.required:
|
||||||
raise ValidationError(self.error_messages['required'])
|
raise ValidationError(self.error_messages['required'])
|
||||||
into[(self.source or field_name)] = None
|
into[(self.source or field_name)] = [] if self.many else None
|
||||||
elif self.many:
|
elif self.many:
|
||||||
into[(self.source or field_name)] = [self.from_native(item) for item in value]
|
into[(self.source or field_name)] = [self.from_native(item) for item in value]
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -18,6 +18,7 @@ import types
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from django.contrib.contenttypes.generic import GenericForeignKey
|
from django.contrib.contenttypes.generic import GenericForeignKey
|
||||||
from django.core.paginator import Page
|
from django.core.paginator import Page
|
||||||
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.forms import widgets
|
from django.forms import widgets
|
||||||
from django.utils.datastructures import SortedDict
|
from django.utils.datastructures import SortedDict
|
||||||
|
@ -912,9 +913,19 @@ class ModelSerializer(Serializer):
|
||||||
|
|
||||||
for field_name, field in self.fields.items():
|
for field_name, field in self.fields.items():
|
||||||
field_name = field.source or field_name
|
field_name = field.source or field_name
|
||||||
|
|
||||||
|
# PY3 compat problem with ManyToManyField & OneToOneField
|
||||||
|
# hasattr in PY2 catches all exceptions, but in PY3 it only looks
|
||||||
|
# for AttributeError
|
||||||
|
# @see: https://code.djangoproject.com/ticket/22839
|
||||||
|
try:
|
||||||
|
field_exists = hasattr(instance, field_name)
|
||||||
|
except (AttributeError, ValueError, ObjectDoesNotExist):
|
||||||
|
field_exists = False
|
||||||
|
|
||||||
if field_name in exclusions \
|
if field_name in exclusions \
|
||||||
and not field.read_only \
|
and not field.read_only \
|
||||||
and (field.required or hasattr(instance, field_name)) \
|
and (field.required or field_exists) \
|
||||||
and not isinstance(field, Serializer):
|
and not isinstance(field, Serializer):
|
||||||
exclusions.remove(field_name)
|
exclusions.remove(field_name)
|
||||||
return exclusions
|
return exclusions
|
||||||
|
|
Loading…
Reference in New Issue
Block a user