2013-02-12 69 views
0

我有一個發佈模型和評論模型,其中包含針對特定帖子所做的評論。通過ForeignKey在Tastypie中進行反向查找

class Post(models.Model): 
    body = models.TextField() 
    user = models.ForeignKey(User) 

class Comment(models.Model): 
    post = models.ForeignKey(Post) 
    date = models.DateTimeField(auto_now_add=True) 
    comment = models.TextField() 
    comment_user = models.ForeignKey(User) 

現在我想讓我的發佈資源將URI包含到附加到特定帖子的所有評論中。

我知道我可以使用fields.ForeignKey來表示我的評論屬於哪個帖子,但是我希望API擁有屬於帖子對象中帖子的所有評論的URI。我希望這是有道理的。

回答

1
class PostResource(ModelResource): 
    comments = fields.ToManyField(CommentResource, 'comments') 

我以前回答過類似的問題。檢查這link

+0

是的,它工作得很好。想你:) 但我最終決定使用ID過濾來獲得與正常的外鍵關係的所有評論。這也給我提供了頁面分頁 – Jonathan 2013-02-13 00:28:36

+0

,你能否看看[這個](http://stackoverflow.com/questions/14818962/how-to-represent-unique-together-in-tastypie)對我來說? – Jonathan 2013-02-13 00:29:25