Is there a way to allow for empty fields within the Django REST serilaizer for Boolean and Int fields.
class InputAttributes(serializers.Serializer):
make = serializers.BooleanField(required=False)
speed = serializers.IntegerField(required=False)
color = serializers.CharField(required=False,allow_blank=True)
I can use the allow_blank for CharFields but not for the others. Based on the above I get,
A valid integer is required
Any ideas ?
"Django REST Framework serializer field required=false" might help you.
Source: "BooleanField"
BooleanField
A boolean representation.
When using HTML encoded form input be aware that omitting a value will always be treated as setting a field to
False
, even if it has adefault=True
option specified. This is because HTML checkbox inputs represent the unchecked state by omitting the value, so REST framework treats omission as if it is an empty checkbox input.Corresponds to
django.db.models.fields.BooleanField
.Signature:
BooleanField()
NullBooleanField
A boolean representation that also accepts
None
as a valid value.Signature:
NullBooleanField()
Corresponds to
django.db.models.fields.NullBooleanField
.
If you're finding this question because you actually want your BooleanField to send/receive null
as a valid value because you've defined
null=True, default=None
in your model's BooleanField
, then define the field in your serializer like this:
myBoolField = serializers.BooleanField(allow_null=True, default=None)
I thought that was what NullBooleanField
did at first but it doesn't, I don't see what it's supposed to do really.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With