代码之家  ›  专栏  ›  技术社区  ›  aviraldg Ortiga

Django-如何从一组“作者”(用户)中筛选具有“作者”的对象?

  •  2
  • aviraldg Ortiga  · 技术社区  · 16 年前

    如何使用“”筛选对象 author 作者 s“( User s) ??

    “对象”是 Post s、 有 ( ForeignKey 到 使用者

    我被这件事难住了,所以我非常感谢你的帮助。当然,我们可以用简单的方式手动过滤它们,但这会严重影响数据库。无论如何,谢谢你。

    员额清单 :

    class Post(models.Model):
        '''A Post or a Status Update.
        '''
        content=models.CharField(max_length=200)
        author=models.ForeignKey(django.contrib.auth.models.User, related_name="author")
        tags=models.ManyToManyField(Tag)
        replyTo=models.ManyToManyField(django.contrib.auth.models.User, related_name="replyTo")
        # Snip model methods
    

    when=models.DateTimeField(auto\u now=True)

    感谢所有帮助回答上一个问题的人。现在我还有最后一件事要问:

    :

    def get_updates():
        return Post.objects.filter(author__in=(list(self.friends.all()) + [self]))
    

    这是获得作者及其朋友所有帖子的最有效方式吗(注意:这是一个幼稚的实现,因为它不处理分页等。稍后将这样做)

    2 回复  |  直到 16 年前
        1
  •  6
  •   Zach    16 年前

    比如:

    Post.objects.filter(author=user)
    

    哪里 user

    编辑

    Post.objects.filter(author__in=users)
    

    其中users是用户集

        2
  •  5
  •   Ignacio Vazquez-Abrams    16 年前
    Post.objects.filter(author__in=setofusers)
    
        3
  •  0
  •   Sreejit Kar    5 年前
    Post.objects.filter(attribute__in = list_of_ids)
    
    推荐文章