Django嵌套对象,不同的序列化器GET和POST [英] Django nested objects, different serializers GET and POST

查看:163
本文介绍了Django嵌套对象,不同的序列化器GET和POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在此处.

我现在可以使用用户主键并在AP序列化器user = UserIndexSerializer()中注释此行后发布新的AP对象:

I can now POST a new AP object using user Primary Key and after commenting this line in the AP serializer user = UserIndexSerializer():

邮递员请求:

{
    "user":1,
    "name":"Max AP 05"
}

但是我现在遇到的问题是,最初的UserIdexSerializer变得无用.

However the problem that I now have is that the initial UserIdexSerializer is rendered useless.

此序列化程序确定要在GET请求中显示的字段,但因此强加了POST请求中所需的字段. 我想做的是:

This serializer determines the fields to show in a GET request but in consequence imposes the fields required in a POST request. What I'm trying to do is:

  • 仅使用用户ID发布新的AP对象
  • 在GET请求期间显示UserIndexSerializer字段(first_name, last_name,但不是ID)
  • POST a new AP object only using the user ID
  • Show the UserIndexSerializer fields during a GET request (first_name, last_name, but not the ID)

我该如何进行这项工作?

How can I make this work?

我已经找到并阅读了此帖子.

I have found and read this post.

我尝试使用不同的视图,一个用于列出我的模型,另一个用于创建新视图:

I tried using different views, one for listing my models and one for creating a new one:

from rest_framework import serializers
from ..models.model_art_piece import AP
from .serializers_user import *


class APIndexSerializer(serializers.ModelSerializer):
    user = UserIndexSerializer()

    class Meta:
        model = AP
        fields = [
            'id',
            'user',
            'name'
        ]

class APCreateSerializer(serializers.ModelSerializer):
    user = UserIDSerializer()

    class Meta:
        model = AP
        fields = [
            'id',
            'user',
            'name'
        ]

    def create(self, validated_data):
        ap = AP.objects.create(**validated_data)
        return ap

class APDetailsSerializer(serializers.ModelSerializer):
    class Meta:
        model = AP
        fields = '__all__'

我还尝试创建不同的序列化器:

And I also tried creating different serializers:

from rest_framework import serializers
from ..models.model_user import User


class UserIndexSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = [
            'first_name',
            'last_name'
        ]

class UserIDSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = [
            'id'
        ]

class UserDetailsSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'

这根本行不通,有人可以帮我吗?

This has not work at all, can anyone help me out with this?

推荐答案

如果我正确理解您想要的是在获取过程中获取嵌套对象.我在序列化程序中解决了同样的问题.

If I understood correctly what you want is to get the nested object during get. I had the same problem which I resolved with this in my serializer.

class APIndexSerializer(serializers.ModelSerializer):
    class Meta:
        model = AP
        fields = ['id','user','name']

    def to_representation(self, obj):
        self.fields['user'] = UserIndexSerializer()
        return super(APIndexSerializer, self).to_representation(obj)

您可以使用id创建并获取用户的嵌套信息.

You can with this create with id and get with nested information of user.

这篇关于Django嵌套对象,不同的序列化器GET和POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆