【发布时间】:2014-08-21 06:13:27
【问题描述】:
我有以下 django 模型
class Cast(models.Model):
coordinates = models.PointField()
class CastImage(models.Model):
image = models.ImageField(upload_to="castimages")
cast = models.ForeignKey(Cast, blank=True, null=True, related_name='images')
和下面的序列化器
class CastSerializer(serializers.ModelSerializer):
images = serializers.RelatedField(many=True)
class Meta:
model = Cast
fields = ('images',)
这一切似乎与文档中的内容非常相似:http://www.django-rest-framework.org/api-guide/relations#relatedfield
但是当我测试 Cast 对象的序列化程序时,虽然这些 Cast 对象确实有图像(我检查过),但我只得到空图像列表:
"results": [
{
"images": []
},
{
"images": []
},
{
"images": []
} ]
我错过了什么?
【问题讨论】:
-
不应该是
model = CastImage吗? -
我编辑了问题以使其更清晰,但不,它真的是 Cast。我想显示具有指向我的 Cast 实例的外键的 CastImage 实例,如下所述:django-rest-framework.org/api-guide/relations#relatedfield
标签: python django django-rest-framework