2014-01-05 145 views
0

我想知道我是怎麼在我的TastyPie API獲得所有相關的動物名單/房屋撥打:Django的反向查找在API

例如:

樓型號:

class House(models.Model): 
    description = models.CharField(max_length=250, blank=True) 
    user = models.ForeignKey(settings.AUTH_USER_MODEL) 

每棟房子可以有以下的一種或多種:

class Dog(models.Model): 
     name = models.CharField(max_length=250) 
     house = models.ForeignKey(House, null=True) 

class Cat(models.Model): 
     name = models.CharField(max_length=250) 
     house = models.ForeignKey(House, null=True) 

class Hamster(models.Model): 
     name = models.CharField(max_length=250) 
     house = models.ForeignKey(House, null=True) 

class Fish(models.Model): 
     name = models.CharField(max_length=250) 
     house = models.ForeignKey(House, null=True) 

這種再轉:

[ 
{ 
id: 1, 
description: "Test Offer22", 
user: { 
date_joined: "2014-01-02T18:39:40", 
email: "[email protected]", 
id: 1, 
is_active: true, 
last_login: "2014-01-05T15:44:30" 
} 
}, 

我想在這個JSON也看到,都涉及到房子,即貓,狗所有動物的名單。這可以在Tastypie中完成嗎?我已閱讀約Lookups that span relationships這是我在這裏後?我不太瞭解你如何從'wrong'方得到關係,如果你知道我的意思是

回答

1

我認爲你正在爲你的related fields尋找full=True argument。例如:

class HouseResource(ModelResource): 
    dogs = fields.ToManyField('DogResource', 'dog_set', full=True)