2012-10-10 25 views
0

我:Django的tastypie包括高清從模型屬性的一個資源上

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的價值?

回答

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

這個「每場脫水的更多信息「,加上像上面這樣的只讀字段應該這樣做。