Angular 7 formData +文件+嵌套对象

时间:2019-02-11 22:54:33

标签: django angular multipartform-data angular7 form-data

我有一个django后端和Angular 7前端,当使用我的参数和文件发送formData时,一切正常,但是当我尝试发送嵌套的json时,它是空的。嵌套的json用于我对象中的嵌套序列化程序。

example with postman

Angular POST

        let input = new FormData();
        var empty = {
            name: "name"
        }
        input.append('username', user.value.username);
        input.append('password', user.value.password);
        if(profile_photo){
            input.append('profile_photo', profile_photo, profile_photo.name);
        }   
        input.append('first_name', user.value.first_name);
        input.append('last_name', user.value.last_name);
        input.append('email', user.value.email);
        input.append('role', user.value.role);
        input.append('active', user.value.active);
        input.append('address', user.value.address);
        input.append('job', user.value.job);
        input.append('phone', user.value.phone);
        input.append('phone_type', user.value.phone_type);
        input.append('superadmin_profile', new Blob([JSON.stringify(empty)], {type : 'application/json'}));
        input.append('general_profile', new Blob([JSON.stringify(empty)], {type : 'application/json'}));
        input.append('security_profile', new Blob([JSON.stringify(empty)], {type : 'application/json'}));

        if(user.value.properties){
            input.append('properties', user.value.properties);
        }
        if(user.value.residentials){
            input.append('residentials', user.value.residentials);
        }

        const httpOptions = {
            headers: new HttpHeaders({
                'Authorization': 'token ' + localStorage.getItem('userToken')
            })
        };
        return this.http.post(this.Endpoints.getUsers, input, httpOptions);

我放置了required = False,所以它没有给出“此字段是必填字段”。但是它仍然是空的,我还在django视图中打印了request.data,并且嵌套的json变量在那里,但是没有进入序列化器。

Django序列化程序

class UserSerializer(serializers.ModelSerializer):
    superadmin_profile = SuperadminProfileSerializer(required=False)
    general_profile = GeneralProfileSerializer(required=False)
    security_profile = SecurityProfileSerializer(required=False)
    residentials_photo = serializers.SerializerMethodField()
    password = serializers.CharField(write_only=True)
    """
    Serializer for Department model.
    """
    class Meta:
        model = User
        fields = [
            'id',
            'password',
            'username',
            'first_name',
            'last_name',
            'email',
            'role',
            'active',
            'address',
            'job',
            'phone_type',
            'phone',
            'superadmin_profile',
            'general_profile',
            'security_profile',
            'residentials',
            'residentials_photo',
            'properties',
            'sections',
            'profile_photo'
        ]

    def create(self, validated_data):
        user = self.context['request'].user
        print validated_data
        superadmin_profile = validated_data.pop('superadmin_profile')
        general_profile = validated_data.pop('general_profile')
        security_profile = validated_data.pop('security_profile')

0 个答案:

没有答案