所以,我有微博系统,每个博客都会通过一些简单的帖子来召集董事会。任何用户都可以在那里创建一个板,并在另一个用户的板上订阅。对于manage subscription,我创建了一个manytomanyfield成员类。问题是如何从数据库中获取用户订阅中所有板的所有帖子。我的模型代码:
class Board (models.Model):
title = models.CharField (max_length=30)
description = models.CharField (max_length=300)
author = models.ForeignKey(User)
created = models.DateTimeField (editable=False)
subscribers = models.ManyToManyField(User, through="Subscription")
class Post (models.Model):
title = models.CharField (max_length=30)
description = models.CharField (max_length=320)
created = models.DateTimeField (editable=False)
board = models.ForeignKey (Board)
class Subscription (models.Model):
subcsriber = models.ForeignKey(User)
board = models.ForeignKey(Board)
sub_date = models.DateTimeField (auto_now_add=True)
所以我有能力在一个回复中获得所有董事会所有排序帖子的列表。或者我应该首先获得所有董事会名单,然后将彼此的所有帖子合并为一个列表,然后按出版日期对其进行排序?
答案 0 :(得分:0)
这应该有效:
Post.objects.filter(board__subscribers=your_user).order_by('created')