Ticket #12753: 12753.diff

File 12753.diff, 5.1 KB (added by dmedvinsky, 7 years ago)
  • django/core/management/commands/loaddata.py

     
    171171                                    if router.allow_syncdb(using, obj.object.__class__):
    172172                                        loaded_objects_in_fixture += 1
    173173                                        models.add(obj.object.__class__)
     174                                        self.fix_auto_values(obj)
    174175                                        obj.save(using=using)
    175176                                loaded_object_count += loaded_objects_in_fixture
    176177                                fixture_object_count += objects_in_fixture
     
    241242        # incorrect results. See Django #7572, MySQL #37735.
    242243        if commit:
    243244            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

     
     1from django.db import models
     2
     3class 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

     
    1010
    1111from django import conf, bin, get_version
    1212from django.conf import settings
     13from django.core import serializers
    1314from django.utils import unittest
    1415
    1516class AdminScriptTestCase(unittest.TestCase):
     
    997998        self.assertNoOutput(err)
    998999        self.assertOutput(out, '0 errors found')
    9991000
     1001
     1002class 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
    10001042##########################################################################
    10011043# COMMAND PROCESSING TESTS
    10021044# Check that user-space commands are correctly handled - in particular,
  • docs/ref/django-admin.txt

     
    353353
    354354When fixture files are processed, the data is saved to the database as is.
    355355Model defined ``save`` methods and ``pre_save`` signals are not called.
     356Nor would ``auto_now`` and ``auto_now_add`` options of ``DateField``,
     357``DateTimeField`` and ``TimeField`` work.
    356358
    357359Note that the order in which fixture files are processed is undefined. However,
    358360all fixture data is installed as a single transaction, so data in
Back to Top