Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rest Framework Django - Disable field to accept null values

Rest Framework Django - Disable field to accept null values

How can I configure for the serialized Model to accept blank fields?

Warning

{"Operacao": ["This field can not be blank."]}

Model

class SEF(models.Model):
    operacao = models.CharField(max_length=10, null=True)
    documento = models.CharField(max_length=50, null=True)
    user = models.ForeignKey(User)

Serializer

class SEFCreateSerializer(serializers.ModelSerializer):

    class Meta:
        model = SEF
        fields = ('operacao', 'documento', 'user')

View

sef_create = SEFCreateSerializer(data=data, many=True)
        if sef_create.is_valid():
            sef_create.save()
            salvo = HTTP_200_OK
        else:
            salvo = sef_create.errors
like image 517
marcelo.delta Avatar asked Feb 16 '26 16:02

marcelo.delta


2 Answers

Include allow_blank=True in the field definition as this:

operacao = serializers.CharField(allow_blank=True, allow_null=True)
like image 105
marcelo.delta Avatar answered Feb 20 '26 07:02

marcelo.delta


class SEFCreateSerializer(ModelSerializer):

    class Meta:
        model = SEF
        fields = ('operacao', 'documento', 'user')
        extra_kwargs = {'operacao': {'required': False}} 
like image 32
Ykh Avatar answered Feb 20 '26 09:02

Ykh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!