Django在自定义管理器中传输数据以进行过滤查询

时间:2016-02-14 09:59:31

标签: django django-models django-queryset django-filter django-managers

我正在尝试制作对话框应用。我可以检查用户是否有来自所有发件人的未读邮件,但我无法过滤每个发件人的未读查询

#model
from .middleware import get_current_user #local copy of user

class ChatManager(models.Manager):
    def unread(self):
        qs = super(ChatManager, self).get_queryset()
        return qs.filter(is_viewed=False, receiver=get_current_user())
    #here need unread_by_sender method with filtering by sender


class Chat(models.Model):
    ....
    sender = models.ForeignKey('accounts.User', verbose_name=u"Sender", related_name="sender")
    receiver = models.ForeignKey('accounts.User', verbose_name=u"Receiver", related_name="receiver")
    is_viewed = models.BooleanField(u"Viewed?", default=False)

    objects = ChatManager()

我想转发用户从模板中过滤经理(发件人),但我不知道如何

#template

{% for user in user_list %}
    {{user.sender.unread.count}}
{% endfor %}

你可以帮我自定义管理器中的unread_by_sender(个人过滤)方法吗? 谢谢。

0 个答案:

没有答案