mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 04:50:12 +03:00
Providing meaningful error message if you forget many=True. Closes #1914.
This commit is contained in:
parent
5b671cb515
commit
7415b74980
|
@ -70,6 +70,7 @@ class BaseSerializer(Field):
|
||||||
self.partial = kwargs.pop('partial', False)
|
self.partial = kwargs.pop('partial', False)
|
||||||
self._context = kwargs.pop('context', {})
|
self._context = kwargs.pop('context', {})
|
||||||
kwargs.pop('many', None)
|
kwargs.pop('many', None)
|
||||||
|
kwargs.pop('many_init', None)
|
||||||
super(BaseSerializer, self).__init__(**kwargs)
|
super(BaseSerializer, self).__init__(**kwargs)
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
|
@ -77,6 +78,14 @@ class BaseSerializer(Field):
|
||||||
# `ListSerializer` classes instead when `many=True` is set.
|
# `ListSerializer` classes instead when `many=True` is set.
|
||||||
if kwargs.pop('many', False):
|
if kwargs.pop('many', False):
|
||||||
return cls.many_init(*args, **kwargs)
|
return cls.many_init(*args, **kwargs)
|
||||||
|
if not kwargs.pop('many_init', False):
|
||||||
|
if not issubclass(cls, ListSerializer):
|
||||||
|
instance = kwargs.get('instance', args[0] if args else None)
|
||||||
|
if isinstance(instance, (list, tuple, models.QuerySet)):
|
||||||
|
msg = (
|
||||||
|
'You have passed a %s as `instance` argument but did '
|
||||||
|
'not set `many=True`.' % instance.__class__.__name__)
|
||||||
|
raise AssertionError(msg)
|
||||||
return super(BaseSerializer, cls).__new__(cls, *args, **kwargs)
|
return super(BaseSerializer, cls).__new__(cls, *args, **kwargs)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -87,7 +96,7 @@ class BaseSerializer(Field):
|
||||||
control which keyword arguments are passed to the parent, and
|
control which keyword arguments are passed to the parent, and
|
||||||
which are passed to the child.
|
which are passed to the child.
|
||||||
"""
|
"""
|
||||||
child_serializer = cls(*args, **kwargs)
|
child_serializer = cls(many_init=True, *args, **kwargs)
|
||||||
list_kwargs = {'child': child_serializer}
|
list_kwargs = {'child': child_serializer}
|
||||||
list_kwargs.update(dict([
|
list_kwargs.update(dict([
|
||||||
(key, value) for key, value in kwargs.items()
|
(key, value) for key, value in kwargs.items()
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from django.db.models.query import QuerySet
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
@ -42,6 +43,18 @@ class TestSerializer:
|
||||||
with pytest.raises(AttributeError):
|
with pytest.raises(AttributeError):
|
||||||
serializer.data
|
serializer.data
|
||||||
|
|
||||||
|
def test_missing_many_kwarg(self):
|
||||||
|
self.Serializer([], many=True)
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
self.Serializer([])
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
self.Serializer(())
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
self.Serializer(QuerySet())
|
||||||
|
self.Serializer(None)
|
||||||
|
self.Serializer(object())
|
||||||
|
self.Serializer({})
|
||||||
|
|
||||||
|
|
||||||
class TestValidateMethod:
|
class TestValidateMethod:
|
||||||
def test_non_field_error_validate_method(self):
|
def test_non_field_error_validate_method(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user