Django Queryset of related objects, after prefiltering on original model

前端 未结 1 881
逝去的感伤
逝去的感伤 2021-02-19 15:44

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 Blo         


        
1条回答
  •  遥遥无期
    2021-02-19 16:03

    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)
    

    0 讨论(0)
提交回复
热议问题