mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #18556 -- Allowed RelatedManager.add() to execute 1 query where possible.
Thanks Loic Bistuer for review.
This commit is contained in:
@@ -247,6 +247,32 @@ class GenericRelationsTests(TestCase):
|
||||
self.comp_func
|
||||
)
|
||||
|
||||
def test_add_bulk(self):
|
||||
bacon = Vegetable.objects.create(name="Bacon", is_yucky=False)
|
||||
t1 = TaggedItem.objects.create(content_object=self.quartz, tag="shiny")
|
||||
t2 = TaggedItem.objects.create(content_object=self.quartz, tag="clearish")
|
||||
# One update() query.
|
||||
with self.assertNumQueries(1):
|
||||
bacon.tags.add(t1, t2)
|
||||
self.assertEqual(t1.content_object, bacon)
|
||||
self.assertEqual(t2.content_object, bacon)
|
||||
|
||||
def test_add_bulk_false(self):
|
||||
bacon = Vegetable.objects.create(name="Bacon", is_yucky=False)
|
||||
t1 = TaggedItem.objects.create(content_object=self.quartz, tag="shiny")
|
||||
t2 = TaggedItem.objects.create(content_object=self.quartz, tag="clearish")
|
||||
# One save() for each object.
|
||||
with self.assertNumQueries(2):
|
||||
bacon.tags.add(t1, t2, bulk=False)
|
||||
self.assertEqual(t1.content_object, bacon)
|
||||
self.assertEqual(t2.content_object, bacon)
|
||||
|
||||
def test_add_rejects_unsaved_objects(self):
|
||||
t1 = TaggedItem(content_object=self.quartz, tag="shiny")
|
||||
msg = "<TaggedItem: shiny> instance isn't saved. Use bulk=False or save the object first."
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
self.bacon.tags.add(t1)
|
||||
|
||||
def test_set(self):
|
||||
bacon = Vegetable.objects.create(name="Bacon", is_yucky=False)
|
||||
fatty = bacon.tags.create(tag="fatty")
|
||||
@@ -266,13 +292,13 @@ class GenericRelationsTests(TestCase):
|
||||
bacon.tags.set([])
|
||||
self.assertQuerysetEqual(bacon.tags.all(), [])
|
||||
|
||||
bacon.tags.set([fatty, salty], clear=True)
|
||||
bacon.tags.set([fatty, salty], bulk=False, clear=True)
|
||||
self.assertQuerysetEqual(bacon.tags.all(), [
|
||||
"<TaggedItem: fatty>",
|
||||
"<TaggedItem: salty>",
|
||||
])
|
||||
|
||||
bacon.tags.set([fatty], clear=True)
|
||||
bacon.tags.set([fatty], bulk=False, clear=True)
|
||||
self.assertQuerysetEqual(bacon.tags.all(), [
|
||||
"<TaggedItem: fatty>",
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user