Opened 4 years ago

Closed 4 years ago

#31433 closed Bug (invalid)

Model.objects.create() doesn't utilize caching for prefetches defined on the ObjectManager.

Reported by: Nathan Yan Owned by: nobody
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords: prefetch cache
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In this example:

class Foo(models.Model):

    objects = FooManager()

class FooManager(models.Manager):

    def get_queryset(self):
        return (...).prefetch_related("bars")


fooA = Foo.objects.get(...)
fooA.bars # already prefetched + cached for later
fooB = Foo.objects.create()
fooB.bars # not prefetched
fooB.bars # queries again because not included in prefetch cache

I would expect fooA and fooB's caches to behave similarly since they're sourced from the same ObjectManager. If it's not a bug, is there a way to specify the same prefetch_related caching functionality for created objects?

Change History (1)

comment:1 by Mariusz Felisiak, 4 years ago

Resolution: invalid
Status: newclosed
Summary: Model.objects.create() doesn't utilize caching for prefetches defined on the ObjectManagerModel.objects.create() doesn't utilize caching for prefetches defined on the ObjectManager.

get_queryset() is not called for new objects created with QuerySet.create(). It's not a bug in Django it's a false assumption that you made in your implementation. Please use one of support channels.

Closing per TicketClosingReasons/UseSupportChannels.

Note: See TracTickets for help on using tickets.
Back to Top