Django Queryset with filtering on reverse foreign key

前端 未结 5 1972
小鲜肉
小鲜肉 2020-12-02 08:38

I have the following Django model:

class Make:
   name = models.CharField(max_length=200)

class MakeContent:
   make = models.ForeignKey(Make)
   published          


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 09:22

    Django doesn't support the select_related() method for reverse foreign key lookups, so the best you can do without leaving Python is two database queries. The first is to grab all the Makes that contain MakeContents where published = True, and the second is to grab all the MakeContents where published = True. You then have to loop through and arrange the data how you want it. Here's a good article about how to do this:

    http://blog.roseman.org.uk/2010/01/11/django-patterns-part-2-efficient-reverse-lookups/

提交回复
热议问题