2015-10-19 47 views
0

我嘗試使用以下串行做一個嵌套寫 -創建()參數**後必須是一個映射,不是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 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上的用戶而不是像例子那樣的ForeignKey?

編輯 - 希望補充一點,即使我得到上述Traceback,它仍會發布給用戶,但不會發布到用戶配置文件。

回答

0

對於任何一個鍵做一個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 

注意以下被刪除for profile_data in profile_data:

原因是沒有多個值。

相關問題