Django中需要解压缩的值太多



我在Django中编辑用户的端点是这样实现的:

@api_view(['PUT'])
@permission_classes([IsAuthenticated])
def updateUser(request, pk):
user = User.objects.get(pk)
data = request.data
user.first_name = data['name']
user.username = data['email']
user.email = data['email']
user.is_staff = data['isAdmin']
user.save()
serializer = UserSerializer(user, many=False)
return Response(serializer.data)

我的用户序列化器是这样实现的:

class UserSerializer(serializers.ModelSerializer):
name = serializers.SerializerMethodField(read_only=True)
_id = serializers.SerializerMethodField(read_only=True)
isAdmin = serializers.SerializerMethodField(read_only=True)
class Meta:
model = User
fields = ['id', '_id', 'username', 'email', 'name', 'isAdmin']
def get__id(self, obj):
return obj.id
def get_isAdmin(self, obj):
return obj.is_staff
def get_name(self, obj):
name = obj.first_name
if name == '':
name = obj.email
return name

我在Redux中向Django发送put请求的操作是这样实现的:

export const updateUser = (user) => async (dispatch, getState) => {
try {
dispatch({
type: USER_UPDATE_REQUEST
})
const {
userLogin: { userInfo
}
} = getState()
const config = {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userInfo.token}`
}
}
const { data } = await axios.put(
`/api/users/update/${user._id}/`,
user,
config
)
dispatch({
type: USER_UPDATE_SUCCESS,
})
dispatch({
type: USER_DETAILS_SUCCESS,
payload: data
})
} catch (error) {
dispatch({
type: USER_UPDATE_FAIL,
payload: error.response && error.response.data.detail
? error.response.data.detail
: error.message,
})
}
}

我调度updateUser动作在我的组件中的按钮点击,像这样:

const submitHandler = (e) => {
e.preventDefault()
dispatch(updateUser({ _id: user._id, name, email, isAdmin }))
}

get error from Django:

Internal Server Error: /api/users/update/undefined/ Traceback (most recent call last):   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesdjangocorehandlersexception.py", line 55, in inner
response = get_response(request)   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesdjangocorehandlersbase.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesdjangoviewsdecoratorscsrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesdjangoviewsgenericbase.py", line 84, in view
return self.dispatch(request, *args, **kwargs)       File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesrest_frameworkviews.py", line 509, in dispatch
response = self.handle_exception(exc)   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesrest_frameworkviews.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesrest_frameworkviews.py", line 480, in raise_uncaught_exception
raise exc   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesrest_frameworkviews.py", line 506, in dispatch
response = handler(request, *args, **kwargs)         File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesrest_frameworkdecorators.py", line 50, in handler
return func(*args, **kwargs)   File "C:UserspcDesktopecommercebackendbaseviewsuser_views.py", line 94, in updateUser
user = User.objects.get(pk)   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesdjangodbmodelsmanager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesdjangodbmodelsquery.py", line 482, in get
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesdjangodbmodelsquery.py", line 1071, in filter
return self._filter_or_exclude(False, args, kwargs)   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesdjangodbmodelsquery.py", line 1089, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesdjangodbmodelsquery.py", line 1096, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesdjangodbmodelssqlquery.py", line 1502, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)   File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesdjangodbmodelssqlquery.py", line 1532, in _add_q
child_clause, needed_inner = self.build_filter(      File "C:UserspcAppDataLocalProgramsPythonPython310libsite-packagesdjangodbmodelssqlquery.py", line 1374, in build_filter
arg, value = filter_expr ValueError: too many values to unpack (expected 2)     [16/Dec/2022 10:32:19] "PUT /api/users/update/undefined/ HTTP/1.1" 500 133599

请告诉我问题出在哪里

它位于视图函数的第一行:

user = User.objects.get(pk=pk)

—说明—

.get需要关键字参数

相关内容

  • 没有找到相关文章

最新更新