mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #21169 -- Reworked RelatedManager methods use default filtering
The `remove()` and `clear()` methods of the related managers created by `ForeignKey`, `GenericForeignKey`, and `ManyToManyField` suffered from a number of issues. Some operations ran multiple data modifying queries without wrapping them in a transaction, and some operations didn't respect default filtering when it was present (i.e. when the default manager on the related model implemented a custom `get_queryset()`). Fixing the issues introduced some backward incompatible changes: - The implementation of `remove()` for `ForeignKey` related managers changed from a series of `Model.save()` calls to a single `QuerySet.update()` call. The change means that `pre_save` and `post_save` signals aren't called anymore. - The `remove()` and `clear()` methods for `GenericForeignKey` related managers now perform bulk delete so `Model.delete()` isn't called anymore. - The `remove()` and `clear()` methods for `ManyToManyField` related managers perform nested queries when filtering is involved, which may or may not be an issue depending on the database and the data itself. Refs. #3871, #21174. Thanks Anssi Kääriäinen and Tim Graham for the reviews.
This commit is contained in:
committed by
Anssi Kääriäinen
parent
0b3c8fc851
commit
17c3997f68
@@ -101,6 +101,22 @@ class Person(models.Model):
|
||||
return "%s %s" % (self.first_name, self.last_name)
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class FunPerson(models.Model):
|
||||
first_name = models.CharField(max_length=30)
|
||||
last_name = models.CharField(max_length=30)
|
||||
fun = models.BooleanField(default=True)
|
||||
|
||||
favorite_book = models.ForeignKey('Book', null=True, related_name='fun_people_favorite_books')
|
||||
favorite_thing_type = models.ForeignKey('contenttypes.ContentType', null=True)
|
||||
favorite_thing_id = models.IntegerField(null=True)
|
||||
favorite_thing = generic.GenericForeignKey('favorite_thing_type', 'favorite_thing_id')
|
||||
|
||||
objects = FunPeopleManager()
|
||||
|
||||
def __str__(self):
|
||||
return "%s %s" % (self.first_name, self.last_name)
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class Book(models.Model):
|
||||
title = models.CharField(max_length=50)
|
||||
@@ -108,10 +124,14 @@ class Book(models.Model):
|
||||
is_published = models.BooleanField(default=False)
|
||||
published_objects = PublishedBookManager()
|
||||
authors = models.ManyToManyField(Person, related_name='books')
|
||||
fun_authors = models.ManyToManyField(FunPerson, related_name='books')
|
||||
|
||||
favorite_things = generic.GenericRelation(Person,
|
||||
content_type_field='favorite_thing_type', object_id_field='favorite_thing_id')
|
||||
|
||||
fun_people_favorite_things = generic.GenericRelation(FunPerson,
|
||||
content_type_field='favorite_thing_type', object_id_field='favorite_thing_id')
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
|
||||
Reference in New Issue
Block a user