简体   繁体   English

如何通过过滤的网桥表字段排序查询集?

[英]How can I order a queryset by a filtered bridge table field?

Given the following models 给定以下模型

class Blog(models.Model):
    followers = models.ManyToManyField(User, through='Follow')

class Follow(models.Model):
    blog = models.ForeignKey(Blog)
    user = models.ForeignKey(User)
    created = models.DateField(auto_now_add=True)

If I have a user, how can I get blogs the user follows, ordered by the date they started following them? 如果我有一个用户,如何获得该用户关注的博客,并按他们开始关注该博客的日期排序?

Try something like this: 尝试这样的事情:

qs = user.blog_set.all().order_by("follow__created")

For descending order use the following: 对于降序,请使用以下命令:

qs = user.blog_set.all().order_by("-follow__created")

I am not sure about "order_by" part, but you can "play" with it. 我不确定“ order_by”部分,但是您可以“玩”它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM