我想检查每个MessageState
中所有Message
的{{1}}(即Threads
),如果有任何线程隐藏了所有消息,那么make那个Thread也是隐藏的(或者从order_threads_message中删除它)。我希望在将视图传递给模板之前在视图中执行此操作。我怎么做?如果你没有得到我,请问。我很乐意解释。请帮助我在视图中如何执行此操作。我会很感激。谢谢。
models.py:
order_threads_message
views.py
class Thread(models.Model):
subject = models.CharField(max_length=50, blank=True, null=True)
user = models.ManyToManyField(User)
class ThreadState(models.Model):
thread = models.ForeignKey(Thread)
user = models.ForeignKey(User)
thread_hidden = models.BooleanField(default=False)
class Message(models.Model):
thread = models.ForeignKey(Thread)
sender = models.ForeignKey(User)
sent_date = models.DateTimeField(default=datetime.now)
body = models.TextField()
class MessageState(models.Model):
message = models.ForeignKey(Message)
user = models.ForeignKey(User)
read = models.BooleanField(default=False)
message_hidden = models.BooleanField(default=False)
答案 0 :(得分:0)
我已针对以下模型运行此查询:
<强>模型强>
class Thread(models.Model):
subject = models.CharField(max_length=50, blank=True, null=True)
user = models.ManyToManyField(User)
class ThreadState(models.Model):
thread = models.ForeignKey(Thread)
user = models.ForeignKey(User)
thread_hidden = models.BooleanField(default=False)
class Message(models.Model):
thread = models.ForeignKey(Thread, related_name = 'message_thread')
sender = models.ForeignKey(User)
sent_date = models.DateTimeField(default=datetime.now)
body = models.TextField()
class MessageState(models.Model):
message = models.ForeignKey(Message, related_name='messagestate_message')
user = models.ForeignKey(User)
read = models.BooleanField(default=False)
message_hidden = models.BooleanField(default=False)
<强>查询强>
Thread.objects.filter(message_thread__in = Message.objects.filter(messagestate_message__in=MessageState.objects.filter(message_hidden=False)))
如果Thread
MessageState
的{{1}}中只有一个message_hidden
为False
,则此查询将返回{{1}}个对象。