django-rest-framework how to make model serializer fields required

The best option according to docs here is to use extra_kwargs in class Meta, For example you have UserProfile model that stores phone number and is required

class UserProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = ('phone_number',)
        extra_kwargs = {'phone_number': {'required': True}} 

For this to work ensure that your models have been set as blank false null false as below.

some_field = models.CharField(blank=False, null=False, default="Anonymous")

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)