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

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

    You need to override the field specifically and add your own validator. You can read here for more detail http://www.django-rest-framework.org/api-guide/serializers/#specifying-fields-explicitly. This is the example code.

    def required(value):
        if value is None:
            raise serializers.ValidationError('This field is required')
    
    class GameRecord(serializers.ModelSerializer):
        score = IntegerField(validators=[required])
    
        class Meta:
            model = Game
    
    0 讨论(0)
  • 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}} 
    
    0 讨论(0)
  • 2020-12-05 15:03

    This is my way for multiple fields. It based on rewriting UniqueTogetherValidator.

    from django.utils.translation import ugettext_lazy as _
    from rest_framework.exceptions import ValidationError
    from rest_framework.utils.representation import smart_repr
    from rest_framework.compat import unicode_to_repr
    
    class RequiredValidator(object):
        missing_message = _('This field is required')
    
        def __init__(self, fields):
            self.fields = fields
    
        def enforce_required_fields(self, attrs):
    
            missing = dict([
                (field_name, self.missing_message)
                for field_name in self.fields
                if field_name not in attrs
            ])
            if missing:
                raise ValidationError(missing)
    
        def __call__(self, attrs):
            self.enforce_required_fields(attrs)
    
        def __repr__(self):
            return unicode_to_repr('<%s(fields=%s)>' % (
                self.__class__.__name__,
                smart_repr(self.fields)
            ))
    

    Usage:

    class MyUserRegistrationSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = User
            fields = ( 'email', 'first_name', 'password' )
            validators = [
                RequiredValidator(
                    fields=('email', 'first_name', 'password')
                )
            ]
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 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}}
    
    0 讨论(0)
提交回复
热议问题