Skip to content

Commit

Permalink
Changed default value for DateTimeField in schema mode #82
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Mar 17, 2015
1 parent 7c676a9 commit c8fcfcf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
5 changes: 2 additions & 3 deletions django_hstore/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
50 changes: 27 additions & 23 deletions tests/django_hstore_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c8fcfcf

Please sign in to comment.