Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow_empty does not work on serializers with many=True. #3361

Closed
pmdarrow opened this issue Sep 3, 2015 · 4 comments · Fixed by #3364
Closed

allow_empty does not work on serializers with many=True. #3361

pmdarrow opened this issue Sep 3, 2015 · 4 comments · Fixed by #3364
Labels
Milestone

Comments

@pmdarrow
Copy link

pmdarrow commented Sep 3, 2015

Is this expected behaviour?

>>> from rest_framework import serializers
>>>
>>> class ChildSerializer(serializers.Serializer):
...     name = serializers.CharField()
...
>>> class ParentSerializer(serializers.Serializer):
...     children = ChildSerializer(many=True, allow_empty=False)
...
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<console>", line 2, in ParentSerializer
  File "/Users/pmdarrow/.virtualenvs/api/lib/python3.4/site-packages/rest_framework/serializers.py", line 96, in __new__
    return cls.many_init(*args, **kwargs)
  File "/Users/pmdarrow/.virtualenvs/api/lib/python3.4/site-packages/rest_framework/serializers.py", line 116, in many_init
    child_serializer = cls(*args, **kwargs)
  File "/Users/pmdarrow/.virtualenvs/api/lib/python3.4/site-packages/rest_framework/serializers.py", line 90, in __init__
    super(BaseSerializer, self).__init__(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'allow_empty'
@xordoquy
Copy link
Collaborator

xordoquy commented Sep 3, 2015

allow_empty isn't part of the Field's core arguments as listed in the documentation so I'd expect allow_empty will raise an exception.
Do you have a use case where this should be a required argument ?

@xordoquy xordoquy closed this as completed Sep 3, 2015
@pmdarrow
Copy link
Author

pmdarrow commented Sep 3, 2015

It's the same use case for allow_empty on a ListField. In my example I want to disallow an empty list for children. To be clear I don't think it should be a required argument, it should be optional.

@xordoquy xordoquy added the Bug label Sep 3, 2015
@xordoquy
Copy link
Collaborator

xordoquy commented Sep 3, 2015

Alright, looks legit. PR to come.

@pmdarrow
Copy link
Author

pmdarrow commented Sep 3, 2015

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants