many=True, allow_null=True allows null instead of the list, not nulls in the list. by delinhabit · Pull Request #2766 · encode/django-rest-framework (original) (raw)
EDIT from @tomchristie
Summary of this:
children = ChildSerializer(many=True, allow_null=True)
Will allow null
instead of a list, but not null
items in the list.
If you want to treat null items in the list as valid, you'll need to instead do this:
children = ListSerializer(child=ChildSerializer(allow_null=True))
This fixes #2673
I tried to keep the test case minimal, but at the same to show a relevant use case. I agree that it might be done differently (suing a different API design), but that will work only for new projects. For existing projects that need to be migrated to DRF >= 3, it's a pain not to be able to just pass allow_null=True
as it was done in DRF 2.x. The good part about this is that the fix is a single line of code changed.