Django rest framework 提交的数据不是文件。请检查drf中表单上的编码类型错误
我已经创建了一个Django rest framework 提交的数据不是文件。请检查drf中表单上的编码类型错误,django-rest-framework,Django Rest Framework,我已经创建了一个模型和序列化程序,但它给出了一个错误提交的数据不是文件检查表单上的编码类型。 模型是- class UserProfile(models.Model): profile_image = models.ImageField(blank=True, upload_to='user/profile_img', null=True,default=None) phone = models.CharField( max_length=20, null=True, blank=True) d
模型
和序列化程序
,但它给出了一个错误提交的数据不是文件检查表单上的编码类型
。
模型是-
class UserProfile(models.Model):
profile_image = models.ImageField(blank=True, upload_to='user/profile_img', null=True,default=None)
phone = models.CharField( max_length=20, null=True, blank=True)
date_of_birth = models.DateField(null=True, blank=True)
city = models.CharField(max_length=50, null=True, blank=True)
country = models.CharField(max_length=50, null=True, blank=True)
zip = models.CharField(max_length=15, null=True, blank=True)
active = models.BooleanField()
user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,related_name='profile')
序列化程序是-
class UserSerializer(serializers.HyperlinkedModelSerializer):
profile = UserProfileSerializer(required=True)
username = serializers.CharField(label="username field",
required=True, allow_null=False,
allow_blank=False)
class Meta:
model = User
fields = ('id', 'username', 'email', 'first_name', 'last_name', 'password', 'profile')
extra_kwargs = {'password': {'write_only': True}, 'first_name': {'required': True},
'last_name': {'required': True}}
def create(self, validated_data):
profile_data = validated_data.pop('profile')
password = validated_data.pop('password')
try:
if User.objects.filter(username__iexact=User(**validated_data).username).exists():
raise serializers.ValidationError("username already exists")
except Exception as e:
error = {'message': ",".join(e.args) if len(e.args) > 0 else 'Unknown Error'}
raise serializers.ValidationError(error)
user = User(**validated_data)
user.set_password(password)
user.save()
UserProfile.objects.create(user=user, **profile_data)
return user
def update(self, instance, validated_data):
profile_data = validated_data.pop('profile')
profile = instance.profile
username = validated_data.get('username', instance.username)
if instance.username != username:
instance.username = validated_data.get('username', instance.username)
try:
if User.objects.filter(username__iexact=instance.username).exists():
raise serializers.ValidationError("username already exists")
except Exception as e:
error = {'message': ",".join(e.args) if len(e.args) > 0 else 'Unknown Error'}
raise serializers.ValidationError(error)
instance.first_name = validated_data.get('first_name', instance.first_name)
instance.last_name = validated_data.get('last_name', instance.last_name)
instance.save()
profile.profile_image = profile_data.get('profile_image', profile.profile_image)
profile.phone = profile_data.get('phone', profile.phone)
profile.date_of_birth = profile_data.get('date_of_birth', profile.date_of_birth)
profile.country = profile_data.get('country', profile.country)
profile.city = profile_data.get('city', profile.city)
profile.zip = profile_data.get('zip', profile.zip)
profile.active = profile_data.get('active', profile.active)
profile.save()
print()
return instance
错误是-
profile: {profile_image: "the submitted data was not a file check the encoding type on the form"}
我不知道是什么导致了这个错误的发生。有人能帮忙吗