From c8fcfcfbd88640ec4c26cbf4dd5cbb5e97d78de7 Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Tue, 17 Mar 2015 14:31:11 +0100 Subject: [PATCH] Changed default value for DateTimeField in schema mode #82 --- django_hstore/virtual.py | 5 ++- tests/django_hstore_tests/tests.py | 50 ++++++++++++++++-------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/django_hstore/virtual.py b/django_hstore/virtual.py index 11b910a..bdd4091 100755 --- a/django_hstore/virtual.py +++ b/django_hstore/virtual.py @@ -97,9 +97,8 @@ def deconstruct(self, *args, **kwargs): return (name, path, args, { 'default': kwargs.get('default')}) # support DateTimeField - if BaseField == models.DateTimeField and kwargs.get('default') is None: - import datetime - kwargs['default'] = datetime.datetime.utcnow() + if BaseField == models.DateTimeField and (kwargs.get('null') or kwargs.get('blank')): + kwargs['default'] = None # support Date and DateTime in django-rest-framework-hstore if BaseField == models.DateTimeField or BaseField == models.DateField: diff --git a/tests/django_hstore_tests/tests.py b/tests/django_hstore_tests/tests.py index 87a4319..775c715 100755 --- a/tests/django_hstore_tests/tests.py +++ b/tests/django_hstore_tests/tests.py @@ -923,29 +923,33 @@ def test_datetime_is_none(self): d = SchemaDataBag.objects.get(name='datetime') self.assertIsNone(d.datetime, None) - def test_migration_datetime(self): - """ issue #82 https://github.com/djangonauts/django-hstore/issues/82 """ - from django.core.management import call_command - from cStringIO import StringIO - # start capturing output - output = StringIO() - sys.stdout = output - call_command('makemigrations', 'django_hstore_tests') - # stop capturing print statements - sys.stdout = sys.__stdout__ - self.assertIn('0001_initial', output.getvalue()) - # check migration file - path = './django_hstore_tests/migrations' - filename = '0001_initial.py' - try: - migration_file = open('{0}/{1}'.format(path, filename)) - except IOError: - path = path.replace('./', './tests/') - migration_file = open('{0}/{1}'.format(path, filename)) - self.assertNotIn("('datetime', django_hstore.virtual.VirtualField(default=datetime.datetime", migration_file.read()) - # delete migration files - import shutil - shutil.rmtree(path) + if django.VERSION[:2] >= (1, 7): + def test_migration_datetime(self): + """ issue #82 https://github.com/djangonauts/django-hstore/issues/82 """ + from django.core.management import call_command + if sys.version_info.major >= 3: + from io import StringIO + else: + from StringIO import StringIO + # start capturing output + output = StringIO() + sys.stdout = output + call_command('makemigrations', 'django_hstore_tests') + # stop capturing print statements + sys.stdout = sys.__stdout__ + self.assertIn('0001_initial', output.getvalue()) + # check migration file + path = './django_hstore_tests/migrations' + filename = '0001_initial.py' + try: + migration_file = open('{0}/{1}'.format(path, filename)) + except IOError: + path = path.replace('./', './tests/') + migration_file = open('{0}/{1}'.format(path, filename)) + self.assertNotIn("('datetime', django_hstore.virtual.VirtualField(default=datetime.datetime", migration_file.read()) + # delete migration files + import shutil + shutil.rmtree(path) else: def test_improperly_configured(self):