Django : select_related with ManyToManyField

前端 未结 2 1763
一生所求
一生所求 2021-02-12 06:33

I have :

class Award(models.Model) :
    name = models.CharField(max_length=100, db_index=True)

class Alias(models.Model) :
    awards = models.ManyToManyField(         


        
相关标签:
2条回答
  • 2021-02-12 06:48

    Django versions 1.4 and above have prefetch_related for this purpose.

    The prefetch_related method is similar to select_related, but does not do a database join. Instead, it executes additional database queries and does the joining in Python.

    0 讨论(0)
  • 2021-02-12 07:00

    If you're not on Django 1.4, there's also the django-batch-select library, which works basically the same way as prefetch_related.

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