考虑这些伪模型:
class Product:
name = charfield
class ProductImage:
image = foreignKey(Product)
还有这个资源
class ProductResource(ModelResource):
images = fields.RelatedField('path.to.resources.ProductImageResource', 'images__all', full=True)
class Meta:
queryset = Product.objects.all()
resource_name = 'products'
返回的 JSON 是:
{
"meta": { ... },
"objects": [
{
"name": "Test",
"images": "[<ProductImage: ProductImage object>, <ProductImage: ProductImage object>]",
}
]
}
当然这没什么用,我只需要列出实例的一些属性。这是否仅适用于脱水方法:
def dehydrate(self, bundle):
bundle.data['images'] = list()
for x in ProductImage.objects.filter(base_product__id=bundle.data['id']):
bundle.data['images'].append(x.thumbnail)
return bundle