【发布时间】:2017-04-02 21:03:00
【问题描述】:
假设,我有一个模型 Collection 和一对多的关系 CollectionImage
class Collection(models.Model):
name = models.CharField(max_length=500)
description = models.TextField(max_length=5000)
publish = models.DateTimeField(auto_now=False, auto_now_add=True)
author = models.CharField(max_length=500)
def __str__(self):
return self.name
class CollectionImage(models.Model):
collection = models.ForeignKey('Collection', related_name='images')
image = models.ImageField(height_field='height_field', width_field='width_field')
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
def __str__(self):
return self.collection.name
我为我的模型创建了一个序列化器类
class CollectionSerializer(ModelSerializer):
class Meta:
model = Collection
fields = [
'id',
'name',
'description',
'publish',
'author',
'images',
]
和一个 API 视图
class CollectionList(ListAPIView):
queryset = Collection.objects.all()
serializer_class = CollectionSerializer
我遇到的问题是,图像字段给出了一个 id 数组,我希望它是一个图像 url 数组,有可能吗?
【问题讨论】:
标签: django django-rest-framework