python - Django Rest Framework,更新嵌套的序列化数据

标签 python django serialization django-rest-framework

我是使用 Django Rest Framework 和序列化数据的新手,我正在尝试更新嵌套序列化程序但遇到了困难,我环顾四周,但大多数人在 serializers.py 方面遇到问题,而我甚至无法在没有错误的情况下进入 def update,这些是我的类(class):

Views.py

from django.shortcuts import render
from rest_framework import generics
from api.serializers import ProfileSerializer
from django.contrib.auth.models import User
from api.models import Profile
from django.http import JsonResponse

class ProfileView(generics.ListCreateAPIView):
    def get(self, request):
        profile = Profile.objects.filter(user_id=request.user.id)
        serializer = ProfileSerializer(profile, many=True)
        return JsonResponse(serializer.data, safe=False)
    def post(self, request, *args, **kwargs):
        profile = Profile.objects.filter(user_id=request.user.id)
        data = request.POST
        serializer = ProfileSerializer(profile, data=data, partial=True)
        if serializer.is_valid():
            serializer.save()
        else:
            print(serializer.errors)
        return JsonResponse(serializer.data, safe=False)

序列化器.py

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

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id','username','first_name','last_name','email','last_login','date_joined','is_active')

class ProfileSerializer(serializers.ModelSerializer):
    user = UserSerializer()
    class Meta:
        model = Profile
        fields = ('id','phone','birth_date','user')

        def update(self, instance, validated_data):
            #Cant get here
            print("hey")
            return instance

样本结构

[
    {
        "id": 3,
        "phone": "XXXXXX",
        "birth_date": "2017-06-29",
        "user": {
            "id": 1,
            "username": "xxxxx",
            "first_name": "XXXXX",
            "last_name": "XXXX",
            "email": "xxxxxxx@gmail.com",
            "last_login": "2017-06-29T15:16:11.438818Z",
            "date_joined": "2017-06-23T16:48:38Z",
            "is_active": true
        }
    }
]

我当前的错误: (发布数据:电话 = 0000000)

AttributeError at /profile/
'QuerySet' object has no attribute '_meta'

谢谢!

最佳答案

发现自己处于类似情况,我能够按以下方式解决。希望对您有所帮助。

def update(self, instance, validated_data):
    user_data = validated_data.pop('user')
    user_serializer = UserSerializer()
    super(self.__class__, self).update(instance,validated_data)
    super(UserSerializer,user_serializer).update(instance.user,user_data)
    return instance

关于python - Django Rest Framework,更新嵌套的序列化数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44830494/

相关文章:

c# - 如何在使用 c# protobuf-net 序列化时将对象附加到文件?

c# - 是否可以使用 Json.Net 以不同的方式序列化对象并反序列化它?

python - 无法在 mac os X 上安装 dlib

django - 在 get_resource_uri 中构建反向 url

python - 如何创建像触发事件一样使用先前行的 Pandas Dataframe?

django - 匿名用户与 django.test.client.login()

javascript - 如何将点击添加到 html 中的产品列表并获取点击到 firestore 的项目

.net - HttpWebRequest 不会序列化

python - 使用 tweepy 在时间间隔内查询推文

python - 从自定义 Django 用户模型中删除密码