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

后端 未结 5 703
佛祖请我去吃肉
佛祖请我去吃肉 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:09

    According to link1 and link2, and due to the intended field is null=True, blank=True (like email field of django.contrib.auth.models.User in my example) this will work:

    class UserSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = User
            fields = ('username', 'email', 'password')
            extra_kwargs = {'email': {'required': True,
                                      'allow_blank': False}}
    

提交回复
热议问题