django按字段过滤

时间:2016-03-02 16:15:41

标签: django filter model django-queryset

我搜索但没有找到答案,也许我无法提出正确的问题,请帮我解决这个问题

我想按非硬编码值进行过滤,但要按每行的连接字段进行过滤

示例:

class Conversation(models.Model):
    user1 = models.ForeignKey(User, related_name="user1_id")
    user2 = models.ForeignKey(User, related_name="user2_id")
    blocked = models.BooleanField(default=False)
    blocked_user = models.ForeignKey(User, blank=True, null=True)
    us1_del = models.DateTimeField(default=datetime.datetime.now())
    us2_del = models.DateTimeField(default=datetime.datetime.now())


class ConversationReply(models.Model):
    conversation = models.ForeignKey(Conversation)
    user = models.ForeignKey(User)
    message = models.TextField()
    datetime = models.DateTimeField(default=datetime.datetime.now())
    seen = models.BooleanField(default=False)

我想过滤每个回复日期gt的位置,而不是会话删除时间。如何做到这一点?

ConversationReply.objects.filter(datetime__gt= conversation__us1_del )

1 个答案:

答案 0 :(得分:0)

我认为您可以使用annotate()

from django.db.models import F
ConversationReply.objects.annotate(conv=F(conversation__us1_del)).filter(datetime__gt=conv)