Optimize django query to pull foreign key and django-taggit relationship

后端 未结 2 1243
逝去的感伤
逝去的感伤 2021-01-06 19:10

I have a todo model defined below:

class Action(models.Model):
    name = models.CharField(\"Action Name\", max_length=200, unique = True)

    complete = mo         


        
2条回答
  •  礼貌的吻别
    2021-01-06 19:26

    The problem is that tags is not a field, but a custom manager, which lives at the class-level and simply does queries.

    I am not sure if this will work on custom managers, as it is meant for many-to-many fields and the like that produce similar query sets. But if you are using django 1.4 you can try the prefetch_related. It will do one more query that batches out the relations and caches them.

    Disclaimer again: I don't know if this works for managers

    actions = Action.objects.select_related('reoccurance').filter(complete=False)\
                    .prefetch_related('tags')
    

提交回复
热议问题