Opened 9 years ago

Last modified 9 years ago

#25693 closed Bug

Data loss if a ManyToManyField is shadowed by Prefetch — at Initial Version

Reported by: Ian Foote Owned by: nobody
Component: Database layer (models, ORM) Version: 1.7
Severity: Release blocker Keywords:
Cc: tom@…, Simon Charette Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With two models:

class Book(Model):
    name = CharField(max_length=100)

class Author(Model):
    books = ManyToManyField(Book)

it is possible to create a prefetch query that loses data from Author.books:

poems = Book.objects.filter(name='Poems')
Author.objects.prefetch_related(
    Prefetch('books', queryset=poems, to_attr='books'),
)

When this queryset is evaluated, each Author's books is overridden by the poems queryset.

Change History (0)

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