commit 6767cafc03f7a0a9e766ccc7a6e4ee3ce6494de8
Author: Jonas Haag <jonas@lophus.org>
Date: Fri Apr 8 03:52:59 2011 -0700
v2 with tests
diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py
index 34f3543..6f523c5 100644
a
|
b
|
from optparse import make_option
|
6 | 6 | |
7 | 7 | from django.conf import settings |
8 | 8 | from django.core import serializers |
| 9 | from django.core.serializers.signals import pre_insert |
9 | 10 | from django.core.management.base import BaseCommand |
10 | 11 | from django.core.management.color import no_style |
11 | 12 | from django.db import connections, router, transaction, DEFAULT_DB_ALIAS |
… |
… |
class Command(BaseCommand):
|
171 | 172 | if router.allow_syncdb(using, obj.object.__class__): |
172 | 173 | loaded_objects_in_fixture += 1 |
173 | 174 | models.add(obj.object.__class__) |
| 175 | pre_insert.send(self, instance=obj.object) |
174 | 176 | obj.save(using=using) |
175 | 177 | loaded_object_count += loaded_objects_in_fixture |
176 | 178 | fixture_object_count += objects_in_fixture |
diff --git a/django/core/serializers/signals.py b/django/core/serializers/signals.py
new file mode 100644
index 0000000..c447e54
-
|
+
|
|
| 1 | from django.dispatch import Signal |
| 2 | |
| 3 | pre_insert = Signal(providing_args=['instance']) |
diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py
index 3dc4ede..ef1508f 100644
a
|
b
|
class TestFixtures(TestCase):
|
363 | 363 | % widget.pk |
364 | 364 | ) |
365 | 365 | |
| 366 | def test_pre_insert_signal(self): |
| 367 | """ |
| 368 | Test for ticket #15696 - deserialized objects may be manipulated before |
| 369 | committing them to database using the pre_insert signal |
| 370 | """ |
| 371 | from django.core.serializers.signals import pre_insert |
| 372 | def callback(signal, sender, instance): |
| 373 | instance.name = 'hello' |
| 374 | pre_insert.connect(callback) |
| 375 | try: |
| 376 | management.call_command( |
| 377 | 'loaddata', |
| 378 | 'thingy.json', |
| 379 | verbosity=0, |
| 380 | commit=False |
| 381 | ) |
| 382 | self.assertEqual(Thingy.objects.get().name, u'hello') |
| 383 | finally: |
| 384 | pre_insert.disconnect(callback) |
| 385 | |
366 | 386 | |
367 | 387 | class NaturalKeyFixtureTests(TestCase): |
368 | 388 | def assertRaisesMessage(self, exc, msg, func, *args, **kwargs): |