我的DRF中保存了以下字段。
我使用react-native作为前端。在更新用户配置文件时,我只想将更改发布到服务器。但是当发布到其余的api时,有必要在formData中发布包括头像的所有值,并且由于头像没有改变,我试图传递现有的uri在服务器上似乎不起作用。 我尝试创建另一个序列化程序并使用PUT将数据传递到服务器,这也无法正常工作。我一直收到错误。
“detail”:“多部分表单解析错误 - 多部分中的无效边界:无”
我认为可能是因为uri通过无效。我也尝试在序列化程序中使用get_validation_exclusions()方法,但它也不起作用。在更新已更改的所有其他字段时,如何使头像可选择发布?
views.py
class ProfileView(APIView):
serializer_class = ProfileSerializer
def get(self, request, format=None):
snippets = Profile.objects.filter(pk=request.user.id)
serializer = ProfileSerializer(snippets, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = ProfileSerializer(data=request.data)
if serializer.is_valid():
user= User.objects.get(pk=request.user.id)
user.profile.bio=request.data["bio"]
user.profile.website=request.data["website"]
user.profile.gender=request.data["gender"]
user.profile.phoneno=request.data["phoneno"]
user.profile.avatar=request.data["avatar"]
user.profile.save()
user.save()
return Response(serializer.data,status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, format=None):
snippet = User.objects.filter(pk=request.user.id)
serializer = ChangeProfileSeralizer(snippet, data=request.data)
if serializer.is_valid():
user= User.objects.get(pk=request.user.id)
user.profile.bio=request.data["bio"]
user.profile.website=request.data["website"]
user.profile.gender=request.data["gender"]
user.profile.phoneno=request.data["phoneno"]
user.profile.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializers.py
class ChangeProfileSeralizer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Profile
fields = ('website', 'phoneno','gender','bio')
class ProfileSerializer(serializers.HyperlinkedModelSerializer):
avatar=serializers.ImageField(max_length=None,use_url=True)
website = serializers.CharField(required=False)
phoneno = serializers.CharField(required=False)
gender = serializers.CharField(required=False)
avatar = serializers.ImageField(required=False)
class Meta:
model = Profile
fields = ('bio','website','phoneno','gender','avatar')
def get_validation_exclusions(self,instance=None):
exclusions = super(ProfileSerializer, self).get_validation_exclusions(instance)
return exclusions +['avatar']
编辑(个人资料模型):
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,primary_key=True)
bio = models.TextField(max_length=500, blank=True)
website = models.TextField(max_length=200, blank=True)
phoneno = models.TextField(max_length=200, blank=True)
gender = models.TextField(max_length=200, blank=True)
avatar = models.ImageField('profile picture', upload_to='images/avatar/', default='/images/avatar/blank.png')
答案 0 :(得分:1)
如果您使用的是Django的模型,那么您需要更改内容
serializers.py
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
exclude = ('user',)
views.py
class ProfileView(views.ModelViewSet):
serializer_class = ProfileSerializer
queryset = Profile.objects.all()
urls.py
router = DefaultRouter()
router.register(r'profile', ProfileView)
现在您可以直接访问您的模型
它是如何工作的你可以看一下:
答案 1 :(得分:0)
如果您想要只读取头像字段,则有两个选项。
选项1
您可以确保不需要@Ruben
所述的ImageFieldclass ProfileSerializer(serializers.HyperlinkedModelSerializer):
avatar = serializers.ImageField(required=False)
选项2
如果该字段存储了图像的字符串表示,并且您计划在前端解析它,那么您只需将头像设为ReadOnlyField。
class ProfileSerializer(serializers.HyperlinkedModelSerializer):
avatar = serializers.ReadOnlyField()
class Meta:
model = Profile
fields = '__all__'