我的序列化器未显示相关模型。我的意思是具有多对一关系的模型。请参见下面的代码。原谅我的英语。
完全使我困惑,请帮忙。我是django的新手。我试图将我的Draft js ContentState保存到数据库。我已将Post模型设为Block模型的Foreignkey。但是,当我尝试使用django-rest-framework序列化程序检索数据时,这些块不会显示。
我尝试自行对它们进行序列化的块也是如此,但不会显示inlineStyleRanges和EntityRanges数据。
#models.py
class Post(models.Model):
created_by= models.ForeignKey(User, on_delete=models.CASCADE)
cat= models.ForeignKey(Category, on_delete=models.CASCADE)
class Block(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
key = models.CharField(max_length=255)
text = models.TextField()
type = models.CharField(max_length=90)
depth = models.IntegerField(default=0)
class InlineStyleRange(models.Model):
block = models.ForeignKey(Block, on_delete=models.CASCADE)
offset = models.IntegerField()
length = models.IntegerField()
style = models.CharField(max_length=90)
class EntityRange(models.Model):
block = models.ForeignKey(Block, on_delete=models.CASCADE)
offset = models.IntegerField()
length = models.IntegerField()
key = models.IntegerField()
class Data(models.Model):
data = models.TextField()
class EntityRangeData(Data):
enityrange = models.ForeignKey(EntityRange, on_delete=models.CASCADE)
#Entity map here we go
class EntityEntry(models.Model):
key = models.IntegerField()
block= models.ForeignKey(Block, on_delete=models.CASCADE)
type = models.CharField(max_length=90)
mutability = models.CharField(max_length=90)
class EntityEntryData(Data):
entityentry = models.ForeignKey(EntityEntry, on_delete=models.CASCADE)
```#Serializers.py```
class EntityEntryDataSerializer(serializers.ModelSerializer):
class Meta:
model = models.EntityEntryData
fields = "__all__"
class EntityEntrySerializer(serializers.ModelSerializer):
data = EntityEntryDataSerializer()
class Meta:
model = models.EntityEntry
fields = "__all__"
class EntityRangeDataSerializer(serializers.ModelSerializer):
class Meta:
model = models.EntityRangeData
fields = "__all__"
class EntityRangeSerializer(serializers.ModelSerializer):
data = EntityRangeDataSerializer()
class Meta:
model = models.EntityRange
fields = "__all__"
class InlineStyleRangeSerializer(serializers.ModelSerializer):
class Meta:
model = models.InlineStyleRange
fields = "__all__"
class BlockSerializer(serializers.ModelSerializer):
inlineStyleRanges = InlineStyleRangeSerializer(many=True, required=False)
entityRanges = EntityRangeSerializer(many=True, required=False)
class Meta:
model = models.Block
fields = "__all__"
class PostSerializer(serializers.ModelSerializer):
blocks = BlockSerializer(many=True, required=False, read_only=True)
class Meta:
model = models.Post
fields = "__all__"
输出类似于
[
{
"id": 1,
"created_by": 1,
"cat": 2
}
]
答案 0 :(得分:0)
Block
模型没有名为inlineStyleRanges
或entityRanges
的属性,因此您应在ForeignKey字段中使用related_name
或指定一个{{1}序列化器中的参数。
source
您可能还必须在inlineStyleRanges = InlineStyleRangeSerializer(many=True, required=False, source='inlinestyle_set')
entityRanges = EntityRangeSerializer(many=True, required=False, source='entityrange_set')
中包括两个嵌套的序列化器。代替BlockSerializer.Meta.fields
,使用列表:
"__all__"