在**之后的create()参数必须是映射,而不是unicode [英] create() argument after ** must be a mapping, not unicode
问题描述
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields =('company','is_admin','last_modified','uuid')
class UserSerializer(serializers.ModelSerializer )
profile = UserProfileSerializer()
class Meta:
model = User
fields =('url','username','email','profile')
def create(self,validated_data):
profile_data = validated_data.pop('profile')
user = User.objects.create(** validated_data)
for profile_data在profile_data中:
UserProfile.objects.create(user = user,** profile_data)
返回用户
但是在做了一个帖子后,我得到以下追溯 -
追溯:
文件/选择/ enterpass_app /lib/python2.7/site-packages/django/core/handlers/base.pyin get_response
132. response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件/ opt /include/app/lib/python2.7/site-packages/django/views/decorators/csrf.pyin wrapped_view
58. return view_func(* args,** kwargs)
文件/ opt /在查看
87. return self.dispatch(request,* args,** kwargs)
文件/ opt / enterpass_app中输入enterpass_app / lib / python2.7 / site-packages / rest_framework / viewsets.py /lib/python2.7/site-packages/rest_framework/views.py在调度
466. response = self.handle_exception(exc)
文件/opt/enterpass_app/lib/python2.7/在dispatch
463中的site-packages / rest_framework / views.pyresponse = handler(request,* args,** kwargs)
文件/opt/enterpass_app/lib/python2.7/site-创建
中的packages / rest_framework / mixins.py 21. self.perform_create(serializer)
文件/ opt / enterpass_app / l ib / python2.7 / site-packages / rest_framework / mixins.pyin perform_create
26. serializer.save()
文件/opt/enterpass_app/lib/python2.7/site-packages/ save_framework / serializers.py保存
180. self.instance = self.create(validated_data)
创建
中的/opt/enterpass/core/serializers.py文件20. UserProfile .objects.create(user = user,** profile_data)
异常类型:/ api / users /
中的TypeError异常值:**之后的create()参数必须是映射,不是unicode
我在这里遵循文档 http:// www。 django-rest-framework.org/api-guide/relations/#writable-nested-serializers
逐字记录,所以不知道我错过了什么。可能我在做一个OneToOneField的用户不是一个ForeignKey像这个例子?
编辑 - 想添加,即使我得到上述追溯它仍然发布对于用户而不是用户个人资料。
对于在密钥上执行OneToOne映射的人来说,这是正确的代码 - p>
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('company','is_admin','last_modified','uuid')
class UserSerializer(serializers.ModelSerializer):
profile = UserProfileSerializer()
class Meta:
model = User
fields =('url','username','email','profile')
def create(self,validated_data):
profile_data = validated_data.pop('profile')
user = User.objects.create(** validated_data)
UserProfile.obje cts.create(user = user,** profile_data)
return user
请注意profile_data中的profile_data的以下内容被删除:
原因是没有多个值。
I'm attempting to do a nested write using the following serializer -
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('company', 'is_admin', 'last_modified', 'uuid')
class UserSerializer(serializers.ModelSerializer):
profile = UserProfileSerializer()
class Meta:
model = User
fields = ('url', 'username', 'email', 'profile')
def create(self, validated_data):
profile_data = validated_data.pop('profile')
user = User.objects.create(**validated_data)
for profile_data in profile_data:
UserProfile.objects.create(user=user, **profile_data)
return user
But after doing a post I get the following traceback -
Traceback:
File "/opt/enterpass_app/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/enterpass_app/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/opt/enterpass_app/lib/python2.7/site-packages/rest_framework/viewsets.py" in view
87. return self.dispatch(request, *args, **kwargs)
File "/opt/enterpass_app/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
466. response = self.handle_exception(exc)
File "/opt/enterpass_app/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
463. response = handler(request, *args, **kwargs)
File "/opt/enterpass_app/lib/python2.7/site-packages/rest_framework/mixins.py" in create
21. self.perform_create(serializer)
File "/opt/enterpass_app/lib/python2.7/site-packages/rest_framework/mixins.py" in perform_create
26. serializer.save()
File "/opt/enterpass_app/lib/python2.7/site-packages/rest_framework/serializers.py" in save
180. self.instance = self.create(validated_data)
File "/opt/enterpass/core/serializers.py" in create
20. UserProfile.objects.create(user=user, **profile_data)
Exception Type: TypeError at /api/users/
Exception Value: create() argument after ** must be a mapping, not unicode
I'm following the documentation here http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers
verbatim so not sure what I'm missing. Possibly that I'm doing a OneToOneField on the User not a ForeignKey like the example?
edit - want to add that even though I get the above Traceback it does still post to User but not to User Profile.
For anyone doing a OneToOne mapping on a key this is the correct code -
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('company', 'is_admin', 'last_modified', 'uuid')
class UserSerializer(serializers.ModelSerializer):
profile = UserProfileSerializer()
class Meta:
model = User
fields = ('url', 'username', 'email', 'profile')
def create(self, validated_data):
profile_data = validated_data.pop('profile')
user = User.objects.create(**validated_data)
UserProfile.objects.create(user=user, **profile_data)
return user
Note that the following is removed for profile_data in profile_data:
Reason being there isn't multiple values.
这篇关于在**之后的create()参数必须是映射,而不是unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!