gpt4 book ai didi

python - ** 之后的 create() 参数必须是映射,而不是 unicode

转载 作者:行者123 更新时间:2023-11-30 23:05:19 25 4
gpt4 key购买 nike

我正在尝试使用以下序列化器进行嵌套写入 -

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

但在发表帖子后,我得到以下回溯 -

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

我正在逐字关注这里的文档http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers所以不确定我是什么'我失踪了。可能我正在用户上执行 OneToOneField,而不是像示例中那样使用外键?

编辑 - 想补充一点,即使我得到上述回溯,它仍然会发布到用户,但不会发布到用户个人资料。

最佳答案

对于任何在键上进行 OneToOne 映射的人来说,这是正确的代码 -

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

请注意,为 profile_data 中的 profile_data 删除了以下内容:

原因是没有多个值。

关于python - ** 之后的 create() 参数必须是映射,而不是 unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33205045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com