我叫Django 2.x
和Django REST Framework
。
我有一个像这样定义的视图集
class UserSettingViewSet(mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):
serializer_class = UserSettingSerializer
permission_classes = (IsAuthenticated, AdminAuthenticationPermission)
def get_object(self):
user_setting = UserSetting.objects.get(user=self.request.user)
return user_setting
def get_queryset(self):
queryset = UserSetting.objects.filter(
user=self.request.user
)
return queryset
def update(self, request, *args, **kwargs):
iso3 = request.POST.get('country', None)
if iso3 is None or not iso3:
raise ValidationError({'country': 'Country iso3 is required'})
try:
country = Country.objects.get(iso3=iso3.upper())
except ObjectDoesNotExist:
raise ValidationError({'country': 'Country does not exists'})
user_setting = UserSetting.objects.get(user=request.user)
user_setting.country = country
user_setting.save()
return super(UserSettingViewSet, self).update(request, *args, **kwargs)
当我从update
调用Angular
时,它不接受表单数据并给出错误消息
{'country': 'Country iso3 is required'}
从邮递员以**表单数据的形式发送数据时工作正常。**
Angular请求的标头是
而邮递员是
如何设置Django在PUT中接受json数据?
答案 0 :(得分:1)
您需要在更新方法中使用dequeue
而不是request.data
。