Fixed #28838 -- Fixed Model.save() crash if the base manager annotates with a related field.

This commit is contained in:
shanghui 2018-01-14 10:58:21 +08:00 committed by Tim Graham
parent cbac11f962
commit 8dc675d90f
3 changed files with 31 additions and 1 deletions

View file

@ -617,6 +617,22 @@ class CustomManagersRegressTestCase(TestCase):
book.refresh_from_db()
self.assertEqual(book.title, 'Hi')
def test_save_clears_annotations_from_base_manager(self):
"""Model.save() clears annotations from the base manager."""
self.assertEqual(Book._meta.base_manager.name, 'annotated_objects')
book = Book.annotated_objects.create(title='Hunting')
Person.objects.create(
first_name='Bugs', last_name='Bunny', fun=True,
favorite_book=book, favorite_thing_id=1,
)
book = Book.annotated_objects.first()
self.assertEqual(book.favorite_avg, 1) # Annotation from the manager.
book.title = 'New Hunting'
# save() fails if annotations that involve related fields aren't
# cleared before the update query.
book.save()
self.assertEqual(Book.annotated_objects.first().title, 'New Hunting')
def test_delete_related_on_filtered_manager(self):
"""Deleting related objects should also not be distracted by a
restricted manager on the related object. This is a regression