在原始模型上进行预过滤后,相关对象的Django Queryset [英] Django Queryset of related objects, after prefiltering on original model

查看:87
本文介绍了在原始模型上进行预过滤后,相关对象的Django Queryset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个模型的查询集,我想获得与外键相关的另一种模型的查询集。以Django项目文档的网络日志架构为例:

Given a queryset for one model, I want to get a queryset of another model that is related by foreign key. Take the Django project docs' weblog schema:

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    def __unicode__(self):
        return self.name

class Author(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField()

    def __unicode__(self):
        return self.name

class Entry(models.Model):
    blog = models.ForeignKey(Blog)
    headline = models.CharField(max_length=255)
    body_text = models.TextField()
    pub_date = models.DateField()
    mod_date = models.DateField()
    authors = models.ManyToManyField(Author)
    n_comments = models.IntegerField()
    n_pingbacks = models.IntegerField()
    rating = models.IntegerField()

    def __unicode__(self):
        return self.headline

假设我有一个 author 对象,我想获取作者编写的每个博客作为查询集。我做类似 author_blogs = [entry.blog以输入author.entry_set] 的操作。但是在这种情况下,我只剩下一个列表,而不是查询集。有没有一种方法可以直接对ORM查询执行此操作,因此可以通过自定义条目管理器使用 use_for_related_fields = True 进行设置,并执行类似 author_blogs = author.entry_set.blogs ,并获得对查询集进行延迟评估等的好处?

Suppose I have an author object, and I want to get every blog that author has written for, as a queryset. I do something like author_blogs = [entry.blog for entry in author.entry_set]. But I'm left with a list in this case, not a queryset. Is there a way I can do this directly with ORM queries, so I can set it up via a custom Entry manager with use_for_related_fields = True and do something like author_blogs = author.entry_set.blogs, and get the benefits of delayed evaluation, etc., of a queryset?

已编辑场景和解决方案

因此,我意识到,我的问题的应用与上面提出的方式稍有不同,但Daniel Roseman的情况很有道理。我的情况实际上更像是 author.entry_set.manager_method()。blogs ,其中 manager_method()返回一个查询集入口对象。我接受了他的回答,因为它启发了我找到的解决方案:

So, I realized after the fact that the application of my question is slightly different than how I posed it above, for which Daniel Roseman's situation makes a lot of sense. My situation is really more like author.entry_set.manager_method().blogs, where manager_method() returns a queryset of Entry objects. I accepted his answer because it inspired the solution I found which is to do:

author_blogs = Blog.objects.filter(entry__in=author.entry_set.manager_method())

有趣的是,它仅使用一个数据库查询。这有点棘手和冗长,所以我认为最好将 blogs()定义为Author的对象方法,并返回上面的内容。

The nice thing is that it only uses one DB query. It's a bit tricky and verbose, so I think it's best to define blogs() as an object method of Author, returning the above.

推荐答案

诀窍是要记住,如果需要Blog的查询集,则应从Blog模型开始。然后,您可以使用双下划线语法来遵循关系。因此:

The trick for this is to remember that if you want a queryset of Blogs, you should start with the Blog model. Then, you can use the double-underscore syntax to follow relations. So:

author_blogs = Blog.objects.filter(entry__authors=author)

这篇关于在原始模型上进行预过滤后,相关对象的Django Queryset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆