Why does django's prefetch_related() only work with all() and not filter()?

前端 未结 3 1214
猫巷女王i
猫巷女王i 2020-11-28 19:30

suppose I have this model:

class PhotoAlbum(models.Model):
    title = models.CharField(max_length=128)
    author = models.CharField(max_length=128)

class          


        
3条回答
  •  庸人自扰
    2020-11-28 20:07

    In Django 1.6 and earlier, it is not possible to avoid the extra queries. The prefetch_related call effectively caches the results of a.photoset.all() for every album in the queryset. However, a.photoset.filter(format=1) is a different queryset, so you will generate an extra query for every album.

    This is explained in the prefetch_related docs. The filter(format=1) is equivalent to filter(spicy=True).

    Note that you could reduce the number or queries by filtering the photos in python instead:

    someAlbums = PhotoAlbum.objects.filter(author="Davey Jones").prefetch_related("photo_set")
    for a in someAlbums:
        somePhotos = [p for p in a.photo_set.all() if p.format == 1]
    

    In Django 1.7, there is a Prefetch() object that allows you to control the behaviour of prefetch_related.

    from django.db.models import Prefetch
    
    someAlbums = PhotoAlbum.objects.filter(author="Davey Jones").prefetch_related(
        Prefetch(
            "photo_set",
            queryset=Photo.objects.filter(format=1),
            to_attr="some_photos"
        )
    )
    for a in someAlbums:
        somePhotos = a.some_photos
    

    For more examples of how to use the Prefetch object, see the prefetch_related docs.

提交回复
热议问题