Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django Rest框架对象不可移植?_Python_Django - Fatal编程技术网

Python Django Rest框架对象不可移植?

Python Django Rest框架对象不可移植?,python,django,Python,Django,我已经序列化了我的一个模型,其中有一个外键。我得到的“父对象”不可编辑 models.py class Parent(models.Model): # Parent data class Child(models.Model): parent = ForeignKey(Parent) 序列化程序.py class ChildSerializers(serializers.ModelSerializer): parent = serializers.RelatedFie

我已经序列化了我的一个模型,其中有一个外键。我得到的
“父对象”不可编辑

models.py

class Parent(models.Model):
    # Parent data

class Child(models.Model):
    parent = ForeignKey(Parent)
序列化程序.py

class ChildSerializers(serializers.ModelSerializer):
    parent = serializers.RelatedField(many=True)
    class Meta:
        model = ReportField
        fields = (
            'id',
            'parent'
        )
api.py

class ChildList(APIView):
    def get(self, request, format=None):
        child = Child.objects.all()
        serialized_child = ChildSerializers(child, many=True)
        return Response(serialized_child.data)
我猜我必须将父列表传递给子列表,但不确定最好的方法

尝试api.py

class ChildList(APIView):
    def get(self, request, format=None):
        child = Child.objects.all()
        parent = Parent.objects.all()
        serialized_child = ChildSerializers(child, many=True)
        serialized_parent = ChildSerializers(parent, many=True)
        return Response(serialized_child.data, serialized_parent.data)

为什么使用多=真。父字段只是一个字段,不需要使用显式序列化器字段。把这些都扔掉就行了


-在评论中回答。

如果您使用的是ModalSerializer,则在一个模式中有两个外键,然后使用如下“to_表示”功能

You can do something like this using python collections as an intermediate

#serializers.py
class TimelineSerializer(serializers.Serializer):
    childs= childSerializer(many=True)
    parents = parentSerializer(many=True)

#apiviews.py
from collections import namedtuple
Timeline = namedtuple('Timeline', ('childs', 'parents'))

def list(self, request):
        timeline = Timeline(
            childs=Child.objects.all(),
            parents=Parent.objects.all(),
        )
        serializer = TimelineSerializer(timeline)
        return Response(serializer.data)
def to_representation(self, instance):
        rep = super().to_representation(instance)
        rep['pcatalog_name'] = CatalogSerializer(instance.pcatalog_name).data
        rep['pcategory_name'] = CategorySerializer(instance.pcategory_name).data
        return rep

将ForeignKey替换为ManyToManyField,以使用many=True澄清序列化程序字段


为什么使用
many=True
Parent
只是一个字段,不需要使用显式序列化器字段。只要去掉这些
many=True
,就行了。小姐理解文件。请把它作为答案输入,这样我就可以标记它了。谢谢。我也以同样的方式误解了文档。是的,只需删除那些many=True,也适用于me@mariodev请你发表评论作为回答好吗。我觉得我对你的回答很满意。