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

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

    Another option is to use required and trim_whitespace if you're using a CharField:

    class CustomObjectSerializer(serializers.Serializer):
        name = serializers.CharField(required=True, trim_whitespace=True)
    

    required doc: http://www.django-rest-framework.org/api-guide/fields/#required trim_whitespace doc: http://www.django-rest-framework.org/api-guide/fields/#charfield

提交回复
热议问题