考虑一下这些伪模型:
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发布于 2012-05-10 20:18:02
你有没有尝试为你的对象定义一个unicode定义,这样它就会打印出你想要的属性,而不是" ProductImage : ProductImage ProductImage“?
https://stackoverflow.com/questions/10530803
复制相似问题