django - 查询对象及其对象字段列表

时间:2015-01-07 09:34:42

标签: python django multithreading django-views

我想检查每个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)

1 个答案:

答案 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_hiddenFalse,则此查询将返回{{1}}个对象。