Django Rest Framework,ModelSerializers和自定义字段验证

Jas*_*enX 1 django django-rest-framework

Using latest version of Django and DRF.

I have a rather complex requirement I can't find a solution for. I'll try to simplify it.

Let's say I have a model that has two fields. field_a and field_b

I have a ModelSerializer for it. I POST a request with its fields. The fields get validated with the model and then against my two serializer functions validate_field_a and validate_field_b. All is well.

Now I'd like my POST request to include a third field that is not a member of that model. let's call it field_c. I have a custom def create(self, validated_data): in my serializer which saves everything to the database.

with regards to field_c I would like to:

  1. Custom Validate it. just like I do with the other two fields.
  2. Require that it is mandatory for the whole request to succeed and if it's not, issue a "Field is required" error just like if I forgot to POST one of my required model fields.
  3. Have the chance to take field_c and save it onto a totally different unrelated Model's row in the db.

I can't seem to get around that. If I add field_c to the fields meta - it throws an exception saying justifiably that field_c is not in my model. If I don't include it in fields, the validate_field_c which I really want to put there doesn't even get called.

What can I do?

Rie*_*uid 5

您可以在序列化程序中将自定义字段添加为write_only字段,并覆盖create方法,以便您可以处理自定义字段的值。

像这样:

class MySerializer(serializers.ModelSerializer):
    field_c = serializers.CharField(write_only=True)

    class Meta:
        model = MyModel
        fields = ('field_a', 'field_b', 'field_c')

    def validate_field_c(self, value):
        if value is 'test':
            raise ValidationError('Invalid')
        return value

    def create(self, validated_data, **kwargs):
        field_c = validated_data.pop('field_c')

        return MyModel.objects.create(**validated_data)
Run Code Online (Sandbox Code Playgroud)