Non-required fields with 'allow_null=True' should not imply a default value by RomuloOliveira · Pull Request #5639 · encode/django-rest-framework (original) (raw)

Prior to the #5518, all fields with required=False and without a explicit default value were not included in the output.

After the patch, even non-required fields now had a default value, which is counter-intuitive if you look at the required documentation, which says Set to false if this field is not required to be present during deserialization.

This PR restore the original required=False behaviour. Now, any required=False without a explicit default value should not be returned in the output.
However, it keeps the implicit default=None for required fields introduced in #5518.

Question I: Did I wrote the test in the correct class (TestNotRequiredOutput) or should it be in the TestDefaultOutput class?

Question II: Is there a reason that the implicit default None value for allow_null is not in the Field.get_default function? Looking at the except handling code, at first it did not sound right.

Question III: Should I add to the documentation that allow_null implicitly set a default=None for required fields?

That issue is already closed and it is not my intention to reopen it, but it's a special case that I did not fully understand why not forcing a explicit default value. E.g, what about allow_blank? And many=True? Shouldn't they have a implicit default value too, using the same logic?