Python Django REST框架:更新给出嵌套序列化程序错误
我有两种型号 验证用户模型和用户配置文件 UseProfile是:Python Django REST框架:更新给出嵌套序列化程序错误,python,django,rest,serialization,django-rest-framework,Python,Django,Rest,Serialization,Django Rest Framework,我有两种型号 验证用户模型和用户配置文件 UseProfile是: class UserProfile(models.Model): user = models.OneToOneField(User, related_name='profile') name = models.CharField(_lazy(u'Name'), max_length=255) 为此,我使用以下序列化程序: from rest_framework import serializers from d
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
name = models.CharField(_lazy(u'Name'), max_length=255)
为此,我使用以下序列化程序:
from rest_framework import serializers
from django.contrib.auth.models import User
from oneanddone.users.models import UserProfile
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('name',)
class UserSerializer(serializers.ModelSerializer):
profile = serializers.RelatedField()
class Meta:
model = User
fields = ('id', 'username', 'email', 'groups', 'profile')
class UserDetailAPI(generics.RetrieveUpdateDestroyAPIView):
"""
API endpoint used to get, update and delete user data.
"""
lookup_field = 'email'
queryset = User.objects.all()
serializer_class = UserSerializer
class UserListAPI(generics.ListCreateAPIView):
"""
API endpoint used to get a complete list of users
and create a new user.
"""
queryset = User.objects.all()
serializer_class = UserSerializer
两个序列化程序的视图为:
class UserListAPI(generics.ListCreateAPIView):
"""
API endpoint used to get a complete list of users
and create a new user.
"""
queryset = User.objects.all()
serializer_class = UserSerializer
class UserDetailAPI(generics.RetrieveUpdateDestroyAPIView):
"""
API endpoint used to get, update and delete user data.
"""
queryset = User.objects.all()
serializer_class = UserSerializer
lookup_field = 'email'
嵌套序列化程序适用于创建/删除查询,但适用于更新查询,如:
pdata = {"username":"testusername", "email":"test@testmail.com","profile":[{"name":"Changed Name"}]}
requests.patch('http://localhost:8000/api/v1/user/test@testmail.com/',data=json.dumps(pdata), headers={'Authorization':'Token bd876bfa04843c6ce1b82c84e27cd510f68dfbbd','Content-Type': 'application/json'}).text
我得到一个错误,说“UserProfile”对象不可编辑。回溯:
这样的更新可以用嵌套的序列化程序来完成吗?还请提供我必须添加的自定义代码,以使其工作。我通过对序列化程序和视图进行以下更改来实现这一点 两种型号的序列化程序:
from django.contrib.auth.models import User
from rest_framework import serializers
from oneanddone.users.models import UserProfile
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('name', 'username', 'privacy_policy_accepted')
class UserSerializer(serializers.ModelSerializer):
profile = UserProfileSerializer(required=False, many=False)
class Meta:
model = User
fields = ('id', 'username', 'email', 'groups', 'profile')
两个序列化程序的视图:
from rest_framework import serializers
from django.contrib.auth.models import User
from oneanddone.users.models import UserProfile
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('name',)
class UserSerializer(serializers.ModelSerializer):
profile = serializers.RelatedField()
class Meta:
model = User
fields = ('id', 'username', 'email', 'groups', 'profile')
class UserDetailAPI(generics.RetrieveUpdateDestroyAPIView):
"""
API endpoint used to get, update and delete user data.
"""
lookup_field = 'email'
queryset = User.objects.all()
serializer_class = UserSerializer
class UserListAPI(generics.ListCreateAPIView):
"""
API endpoint used to get a complete list of users
and create a new user.
"""
queryset = User.objects.all()
serializer_class = UserSerializer
更新查询可以写在类似的行上,如:
# Change Profile Data(name, username, privacy_policy_accepted)
changed_data = {'username': 'testname', 'email': 'test@test.com',
'profile': {'name': 'Changed Test Name', 'username': 'testname123', 'privacy_policy_accepted': False}}
response = self.client.patch(user_uri, changed_data, format='json')
免责声明:所有这些代码片段现在都是Mozilla组织下的应用程序的一部分,并根据Mozilla许可证发布
可在此处找到指向原始代码的链接:
,你有没有用过这个?我也有类似的经历issue@SuperDuperTango是的,我通过对序列化程序和视图进行一些更改使其正常工作。添加了一个答案,包括实际的代码片段。希望这会有帮助。我是根据这个反应来做的。但是,我不能使用该代码,因为如果“UserProfileSerializer”具有“fields”参数,则在启动时会发生崩溃。我可以通过做相反的操作并使用“排除”来解决这个问题。我在这里提出了一个问题