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

后端 未结 5 708
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 14:14

I have a model that I\'m filling out step by step, it means I\'m making a form wizard.

Because of that most fields in this model are required but have null=Tru

5条回答
  •  忘掉有多难
    2020-12-05 15:02

    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}} 
    

提交回复
热议问题