Django - 一对一序列化器创建功能

时间:2016-12-09 14:54:21

标签: python django serialization django-models django-rest-framework

我将默认用户模型扩展为ExtendedUser:

from django.db import models
from django.contrib.auth.models import User

class ExtendedUser(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    shirt_size = models.CharField(max_length=2)

用户序列化程序:

from django.contrib.auth.models import User
from rest_framework import serializers


class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'groups', 'is_staff')

ExtendedUser序列化程序:

from api.resources.users.models.extended_user import ExtendedUser
from rest_framework import serializers
from django.contrib.auth.models import User
from api.resources.users.serializers.user import UserSerializer


class ExtendedUserSerializer(serializers.HyperlinkedModelSerializer):
    user = UserSerializer(read_only=False)

    class Meta:
        model = ExtendedUser
        fields = ('url', 'shirt_size', 'user')

    def create(self, validated_data):
        user_data = validated_data.pop('user')
        user = User.objects.create(**user_data)
        return ExtendedUser.objects.create(user=user, **validated_data)

主要结果应该是在提交新的ExtendedUser时,它也会创建一个用户一对一的反射。

但是我收到了这个错误:

  

用户:myusername在此之前需要具有字段“user”的值   可以使用多对多关系。

回溯:

Environment:


Request Method: POST
Request URL: http://localhost:8000/users/

Django Version: 1.10.4
Python Version: 2.7.12
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'api']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Python27\lib\site-packages\django\core\handlers\exception.py" in inner
  39.             response = get_response(request)

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Python27\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "C:\Python27\lib\site-packages\rest_framework\viewsets.py" in view
  83.             return self.dispatch(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
  477.             response = self.handle_exception(exc)

File "C:\Python27\lib\site-packages\rest_framework\views.py" in handle_exception
  437.             self.raise_uncaught_exception(exc)

File "C:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
  474.             response = handler(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\rest_framework\mixins.py" in create
  21.         self.perform_create(serializer)

File "C:\Python27\lib\site-packages\rest_framework\mixins.py" in perform_create
  26.         serializer.save()

File "C:\Python27\lib\site-packages\rest_framework\serializers.py" in save
  214.             self.instance = self.create(validated_data)

File "C:/Users/ozbar/PycharmProjects/usnccm/usnccm-api\api\resources\users\serializers\extended_user.py" in create
  15.         user = User.objects.create(**user_data)

File "C:\Python27\lib\site-packages\django\db\models\manager.py" in manager_method
  85.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Python27\lib\site-packages\django\db\models\query.py" in create
  397.         obj = self.model(**kwargs)

File "C:\Python27\lib\site-packages\django\contrib\auth\base_user.py" in __init__
  68.         super(AbstractBaseUser, self).__init__(*args, **kwargs)

File "C:\Python27\lib\site-packages\django\db\models\base.py" in __init__
  550.                             setattr(self, prop, kwargs[prop])

File "C:\Python27\lib\site-packages\django\db\models\fields\related_descriptors.py" in __set__
  499.         manager = self.__get__(instance)

File "C:\Python27\lib\site-packages\django\db\models\fields\related_descriptors.py" in __get__
  476.         return self.related_manager_cls(instance)

File "C:\Python27\lib\site-packages\django\db\models\fields\related_descriptors.py" in __init__
  783.                                  (instance, self.source_field_name))

Exception Type: ValueError at /users/
Exception Value: "<User: oz>" needs to have a value for field "user" before this many-to-many relationship can be used.

通过django-rest web视图在POST上验证_data对象的值:

{u'user': OrderedDict([(u'username', u'oz'), (u'email', u'oz.barshalom@gmail.com'), (u'groups', []), (u'is_staff', True)]), u'shirt_size': u'm'}

1 个答案:

答案 0 :(得分:1)

好的首先,问题与django-rest-framework或你的python版本无关。

似乎是django == 1.10的一个问题,因为我只是尝试了:

User.objects.create(user="hello", email="333.22@eewofw.com", groups=[], is_staff=False)

在django的shell中收到完全相同的错误。但是,如果我们尝试更新版本的django,问题就不会持续存在。

我注意到使用此命令安装django时:

pip install django

它将安装django==1.10而不是最新版本。如果你错误地安装了这个旧版本,我建议卸载django,然后安装最新版本:

pip uninstall django
pip install django==1.9.12

使用django==1.9.12时,您需要完全从groups删除UserSerializer,而在执行POST时不提供。{/ p>