gpt4 book ai didi

python - Django - 一对一序列化器创建函数

转载 作者:行者123 更新时间:2023-11-28 18:26:31 25 4
gpt4 key购买 nike

我将默认用户模型扩展为 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 时,它也会创建一个具有一对一关系的用户。

但是我收到了这个错误:

User: myusername needs to have a value for field "user" before this many-to-many relationship can be used.

回溯:

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 WebView 在 POST 上验证数据对象的值:

{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'}

最佳答案

好的,对于初学者来说,问题与 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 安装 django

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

pip 卸载 django
pip install django==1.9.12

当使用 django==1.9.12 时,您需要从 UserSerializer 中完全删除 groups 并且在执行您的 POST

关于python - Django - 一对一序列化器创建函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41063110/

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