[英]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.