Ticket #12753: 12753.diff
File 12753.diff, 5.1 KB (added by , 14 years ago) |
---|
-
django/core/management/commands/loaddata.py
171 171 if router.allow_syncdb(using, obj.object.__class__): 172 172 loaded_objects_in_fixture += 1 173 173 models.add(obj.object.__class__) 174 self.fix_auto_values(obj) 174 175 obj.save(using=using) 175 176 loaded_object_count += loaded_objects_in_fixture 176 177 fixture_object_count += objects_in_fixture … … 241 242 # incorrect results. See Django #7572, MySQL #37735. 242 243 if commit: 243 244 connection.close() 245 246 def fix_auto_values(self, obj): 247 for f in obj.object._meta.fields: 248 if getattr(f, 'auto_now', False): 249 f.auto_now = False 250 if getattr(f, 'auto_now_add', False): 251 f.auto_now_add = False -
tests/regressiontests/admin_scripts/app_with_fixtures/fixtures/auto_now_add.json
1 [ 2 { 3 "model": "app_with_fixtures.entry", 4 "pk": 1, 5 "fields": { 6 "content": "May the Force be with you." 7 } 8 } 9 ] -
tests/regressiontests/admin_scripts/app_with_fixtures/models.py
1 from django.db import models 2 3 class Entry(models.Model): 4 content = models.CharField(max_length=64) 5 pub_date = models.DateTimeField(auto_now_add=True) -
tests/regressiontests/admin_scripts/tests.py
10 10 11 11 from django import conf, bin, get_version 12 12 from django.conf import settings 13 from django.core import serializers 13 14 from django.utils import unittest 14 15 15 16 class AdminScriptTestCase(unittest.TestCase): … … 997 998 self.assertNoOutput(err) 998 999 self.assertOutput(out, '0 errors found') 999 1000 1001 1002 class ManageLoadData(AdminScriptTestCase): 1003 """ 1004 Tests loaddata command. 1005 """ 1006 def setUp(self): 1007 self.write_settings('settings.py', 1008 apps=['admin_scripts.app_with_fixtures'], 1009 sdict={'DEBUG': True}) 1010 1011 def tearDown(self): 1012 self.remove_settings('settings.py') 1013 test_dir = os.path.dirname(os.path.dirname(__file__)) 1014 for format in serializers.get_public_serializer_formats(): 1015 try: 1016 os.unlink(os.path.join(test_dir, 'initial_data.%s' % format)) 1017 except: 1018 pass 1019 1020 def _make_initial_data(self, name): 1021 format = name.rpartition('.')[2] 1022 test_dir = os.path.dirname(os.path.dirname(__file__)) 1023 src = os.path.join(test_dir, 'admin_scripts', 'app_with_fixtures', 1024 'fixtures', name) 1025 dst = os.path.join(test_dir, 'initial_data.%s' % format) 1026 shutil.copyfile(src, dst) 1027 1028 def test_date_auto_now_add(self): 1029 """ 1030 Tests that syncdb fails on loaddata stage, when there is a model with 1031 DateField, which has auto_now_add=True, but the fixtures don't have the 1032 value for that field. 1033 1034 See #12753. 1035 """ 1036 self._make_initial_data('auto_now_add.json') 1037 args = ['syncdb', '--noinput'] 1038 out, err = self.run_manage(args) 1039 self.assertOutput(err, 'IntegrityError') 1040 1041 1000 1042 ########################################################################## 1001 1043 # COMMAND PROCESSING TESTS 1002 1044 # Check that user-space commands are correctly handled - in particular, -
docs/ref/django-admin.txt
353 353 354 354 When fixture files are processed, the data is saved to the database as is. 355 355 Model defined ``save`` methods and ``pre_save`` signals are not called. 356 Nor would ``auto_now`` and ``auto_now_add`` options of ``DateField``, 357 ``DateTimeField`` and ``TimeField`` work. 356 358 357 359 Note that the order in which fixture files are processed is undefined. However, 358 360 all fixture data is installed as a single transaction, so data in