【发布时间】:2018-08-12 17:05:38
【问题描述】:
我在 vue 中发送带有表单数据的 axios.patch 请求
axios.patch(`${API_BASE}/products/${id}/`, data, {
headers: { 'Content-Type': 'multipart/form-data'
}
并调用 Django ModelViewset 部分更新
class MyViewSet(viewsets.ModelViewSet):
def update(self, request, *args, **kwargs):
data = request.data.copy()
question = self.get_object()
...
问题是我以字符串形式获取所有值.. null 值作为“null”,整数值作为“1”,依此类推。 enter image description here
如何在 request.data 中获取正常值(null 为 None,整数为 int)?
【问题讨论】:
-
使用序列化器并在序列化器初始化中设置
partial=True。例如:serializer = YourSerializer(request, data=request.data, partial=True) -
@zaidfazil 感谢您的回复,但问题是关于“我在 request.data 中接收空值作为'null'类型的字符串。我应该怎么做才能在请求中接收空值作为 None .data?
-
request.data始终是QueryDict,。序列化器用于解释请求中的数据。为您的数据编写一个序列化程序,序列化程序中的验证数据将是您可以使用的。 -
@zaidfazil 知道了,谢谢!
标签: vue.js django-rest-framework django-rest-viewsets