Skip to content

Commit

Permalink
Fixed #35073 -- Avoided unnecessary calling of callables used by SET/…
Browse files Browse the repository at this point in the history
…SET_DEFAULT in Collector.collect().
  • Loading branch information
bcail authored Feb 8, 2024
1 parent 1b5338d commit 9c5e382
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
6 changes: 2 additions & 4 deletions django/db/models/deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ def set_on_delete(collector, field, sub_objs, using):
def set_on_delete(collector, field, sub_objs, using):
collector.add_field_update(field, value, sub_objs)

set_on_delete.lazy_sub_objs = True

set_on_delete.deconstruct = lambda: ("django.db.models.SET", (value,), {})
set_on_delete.lazy_sub_objs = True
return set_on_delete


Expand All @@ -76,9 +77,6 @@ def SET_DEFAULT(collector, field, sub_objs, using):
collector.add_field_update(field, field.get_default(), sub_objs)


SET_DEFAULT.lazy_sub_objs = True


def DO_NOTHING(collector, field, sub_objs, using):
pass

Expand Down
22 changes: 19 additions & 3 deletions tests/delete_regress/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ class Item(models.Model):
location_value = models.ForeignKey(
Location, models.SET(42), default=1, db_constraint=False, related_name="+"
)
location_default = models.ForeignKey(
Location, models.SET_DEFAULT, default=1, db_constraint=False, related_name="+"
)


# Models for #16128
Expand Down Expand Up @@ -151,3 +148,22 @@ class OrderedPerson(models.Model):

class Meta:
ordering = ["name"]


def get_best_toy():
toy, _ = Toy.objects.get_or_create(name="best")
return toy


def get_worst_toy():
toy, _ = Toy.objects.get_or_create(name="worst")
return toy


class Collector(models.Model):
best_toy = models.ForeignKey(
Toy, default=get_best_toy, on_delete=models.SET_DEFAULT, related_name="toys"
)
worst_toy = models.ForeignKey(
Toy, models.SET(get_worst_toy), related_name="bad_toys"
)
14 changes: 11 additions & 3 deletions tests/delete_regress/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,17 @@ def test_set_querycount(self):
Item.objects.create(
version=version,
location=location,
location_default=location,
location_value=location,
)
# 3 UPDATEs for SET of item values and one for DELETE locations.
with self.assertNumQueries(4):
# 2 UPDATEs for SET of item values and one for DELETE locations.
with self.assertNumQueries(3):
location.delete()


class SetCallableCollectorDefaultTests(TestCase):
def test_set(self):
# Collector doesn't call callables used by models.SET and
# models.SET_DEFAULT if not necessary.
Toy.objects.create(name="test")
Toy.objects.all().delete()
self.assertSequenceEqual(Toy.objects.all(), [])

0 comments on commit 9c5e382

Please sign in to comment.