简体   繁体   English

如何编写django-rest-framework序列化程序以保存包含通用模型的嵌套层次结构?

[英]How do I write a django-rest-framework serializer to save a nested hierarchy containing a generic model?

I am using django 1.6 and django-rest-framework 2.4. 我正在使用django 1.6和django-rest-framework 2.4。 I have a generic model that has a foreign key relationship to another model. 我有一个通用模型,该模型与另一个模型具有外键关系。 The generic model can obviously be associated with any other host model. 通用模型显然可以与任何其他宿主模型相关联。 I want to be able to save the host model and the generic model with all its associations in one go. 我希望能够一次性保存主机模型和通用模型及其所有关联。

For example: 例如:

models.py: models.py:

from django.db import models
from django.contrib.contenttypes.generic import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType


class GenericItem(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

    details = models.TextField(blank=True)


class GenericSubItem(models.Model):
    generic_item = models.ForeignKey(GenericItem, null=True, blank=True, related_name="sub_items")

    details = models.TextField(blank=True)


class Host(models.Model):
    details = models.TextField(blank=True)

    generic_items = GenericRelation(GenericItem, content_type_field='content_type', object_id_field='object_id')

serializers.py: serializers.py:

from rest_framework import serializers
from . import models


class GenericSubItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.GenericSubItem
        fields = ('id', 'details', )


class GenericItemSerializer(serializers.ModelSerializer):
    sub_items = GenericSubItemSerializer(many=True, required=False, allow_add_remove=True)

    class Meta:
        model = models.GenericItem
        fields = ('id', 'details', 'sub_items', )


class HostSerializer(serializers.ModelSerializer):
    generic_items = GenericItemSerializer(many=True, required=False, allow_add_remove=True)

    class Meta:
        model = models.Host
        fields = ('id', 'details', 'generic_items', )

views.py: views.py:

from rest_framework import viewsets
from . import serializers, models


class HostView(viewsets.ModelViewSet):
    queryset = models.Host.objects.all()
    serializer_class = serializers.HostSerializer

The problem here is that the sub_items don't get saved, even though they are posted. 这里的问题是即使发布了sub_items也不会保存它们。 Even worse, if you add a sub item through the shell, it will display it fine but when you save, the sub item is deleted. 更糟糕的是,如果通过外壳添加子项,它将很好地显示,但是保存后,该子项将被删除。

How should the serializer be written so that saving the host will save everything else? 应该如何编写序列化程序,以便保存主机将保存其他所有内容?

Thanks, 谢谢,

Paul 保罗

Could you please give more details about your views.py ? 您能否提供有关您的views.py更多详细信息? Or you can have a try to override your create(self, validated_data) in your GenericItemSerializer as follow: 或者,您可以尝试在GenericItemSerializer覆盖您的create(self, validated_data) ,如下所示:

class GenericItemSerializer(serializers.ModelSerializer):
    sub_items = GenericSubItemSerializer(many=True, required=False, allow_add_remove=True)

    class Meta:
        model = models.GenericItem
        fields = ('id', 'details', 'sub_items', )

    def create(self, validate_data):
        subitemdata = validate_data.pop('sub_items')
        subitemobj = GenericSubItem.objects.create(**subitemdata)
        GenericItem.objects.create(sub_items=subitemobj, **validate_data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django-rest-framework 中的可写嵌套序列化程序? - Writable nested serializer in django-rest-framework? django-rest-framework 如何使模型序列化器字段成为必需 - django-rest-framework how to make model serializer fields required 如何使用django-rest-framework在序列化程序级别上扩展模型 - How to extend model on serializer level with django-rest-framework 如何在Django-rest-framework中序列化列表? - how can I serializer a list in django-rest-framework? 保存前编辑 django-rest-framework 序列化程序 object - Editing django-rest-framework serializer object before save django-rest-framework 3.0在嵌套序列化程序中创建或更新 - django-rest-framework 3.0 create or update in nested serializer 如何在Django-rest-framework序列化器中的关系模型中获得额外的列? - How to get an extra column in relational model in Django-rest-framework serializer? 如何使用Django-rest-framework创建与模型不同的视图 - How do I create a view different than the model with Django-rest-framework 如何在 django-rest-framework 的序列化器中使用时区序列化时间? - how to serialize time with timezone in django-rest-framework's serializer? 如何在django-rest-framework中将参数传递给序列化器? - How to pass arguments to a Serializer in django-rest-framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM