Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement prefetching for ClusterTaggableManager #180

Merged
merged 1 commit into from
Jan 3, 2024

Conversation

chosak
Copy link
Member

@chosak chosak commented Dec 22, 2023

This commit implements prefetching for ClusterTaggableManager so that prefetch_related works properly:

class TaggedPlace(TaggedItemBase):
    content_object = ParentalKey(
        "Place",
        related_name="tagged_items",
        on_delete=models.CASCADE,
    )

class Place(ClusterableModel):
    tags = ClusterTaggableManager(
        through=TaggedPlace,
        blank=True,
    )

places = Place.objects.prefetch_related("tags").all()
for place in places:
    place.tags.all()

Currently the above code functions properly but does not actually leverage the prefetching; each call to place.tags.all() re-queries the database for each place's tags, instead of using the list previously fetched with the prefetch_related("tags").

This commit properly implements prefetch_related to avoid those duplicate queries.

To test, run tox.

This change requires a bit of complexity due to a change in Django 5.0+ (which is tested under tox but not currently tested in GitHub Actions): As documented in https://code.djangoproject.com/ticket/31486, it's no longer possible to pass unsaved model instances to related filters, preventing calls like:

TaggedPlace.objects.filter(content_object=Place())

This requires some special handling to continue to support querying tags on unsaved model instances:

place = Place()
place.tags.count()

Please see comments in taggit.py for additional detail.

Closes #38, and may also address wagtail/wagtail#6044.

This commit implements prefetching for ClusterTaggableManager so that
prefetch_related works properly:

class TaggedPlace(TaggedItemBase):
    content_object = ParentalKey(
        "Place",
        related_name="tagged_items",
        on_delete=models.CASCADE,
    )

class Place(ClusterableModel):
    tags = ClusterTaggableManager(
        through=TaggedPlace,
        blank=True,
    )

places = Place.objects.prefetch_related("tags").all()
for place in places:
    place.tags.all()

Currently the above code functions properly but does not actually
leverage the prefetching; each call to place.tags.all() re-queries the
database for each place's tags, instead of using the list previously
fetched with the prefetch_related("tags").

This commit properly implements prefetch_related to avoid those
duplicate queries.

To test, run tox.

This change requires a bit of complexity due to a change in Django 5.0+
(which is tested under tox but not currently tested in GitHub Actions):
As documented in https://code.djangoproject.com/ticket/31486, it's no
longer possible to pass unsaved model instances to related filters,
preventing calls like:

TaggedPlace.objects.filter(content_object=Place())

This requires some special handling to continue to support querying tags
on unsaved model instances:

place = Place()
place.tags.count()

Please see comments in taggit.py for additional detail.
Copy link
Contributor

@gasman gasman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good to me. Thanks for the detailed comments!

@gasman gasman merged commit e810af9 into wagtail:main Jan 3, 2024
@chosak chosak deleted the feature/prefetch-tags branch January 3, 2024 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cluster TaggableManager is doing too much queries
2 participants