2014-02-10 42 views
1

我有這樣一個模型:如何根據Tastypie中的孩子的孩子字段進行過濾?

Shop 
    Categories 
     Items 

我可以過濾店的類別是這樣的:

categories = fields.ToManyField('api.CategoryResource', attribute=lambda bundle: Category.objects.filter(parent__isnull=True), full= True) 

比方說,我想,當我查詢店鋪來過濾發佈的項目。

我該如何過濾?我應該在哪裏寫查詢?

items = fields.ToManyField(ItemResource, attribute=lambda bundle: Item.objects.filter(category=bundle.obj, published=True),related_name='items', full=True) 

這個代碼給我錯誤:

{"error": "The model '<Category: category1>' has an empty attribute '<function <lambda> at 0x10ba506e0>' and doesn't allow a null value."} 

回答

0

原來,當由拉姆達函數返回的列表爲空,出現此錯誤。 因此,添加null = True將解決此問題。

所以,你最終這個:

items = fields.ToManyField(ItemResource, attribute=lambda bundle: Item.objects.filter(category=bundle.obj, published=True),related_name='items', full=True, null=True)