我有:
class Item(models.Model):
# some fields like name, quantity
def get_first_image(self):
instance = ItemImage.objects.filter(parent_id = self.id)[0]
return instance.images
class ItemImage(models.Model):
# parent with foreignkey to Item and an ImageField to store images
如何使用tastypie检索get_first_image的值?
答案 0 :(得分:1)
您可以尝试将自定义资源与每个字段的dehyration一起用于您要“合成”的字段,例如
# Imports etc omitted
class ItemResource(ModelResource):
# some fields like name, quantity
first_image = fields.ImageField(readonly=True)
def dehydrate_first_image(self):
instance = ItemImage.objects.filter(parent_id = self.id)[0]
return instance.images
您可以获取有关资源in the tastypie resources documentation
的更多信息这个“每个字段脱水”,加上一个只读字段,就像你上面那样,应该这样做。