Skip to content

Commit 4a16479

Browse files
committed
Skip monkey-patching for South with Django 1.7
This fixes the following error when South tries to access `management._commands`, which has been dropped in Django 1.7: …/pytest_django/pytest_django/fixtures.py:41: in _django_db_setup _handle_south() …/pytest_django/pytest_django/fixtures.py:128: in _handle_south patch_for_test_db_setup() _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ def patch_for_test_db_setup(): # Load the commands cache management.get_commands() # Repoint to the correct version of syncdb if hasattr(settings, "SOUTH_TESTS_MIGRATE") and not settings.SOUTH_TESTS_MIGRATE: # point at the core syncdb command when creating tests # tests should always be up to date with the most recent model structure management._commands['syncdb'] = 'django.core' else: > management._commands['syncdb'] = MigrateAndSyncCommand() E AttributeError: 'module' object has no attribute '_commands' …/site-packages/south/management/commands/__init__.py:34: AttributeError
1 parent 76cb458 commit 4a16479

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

pytest_django/fixtures.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -94,39 +94,43 @@ def flushdb():
9494

9595
def _handle_south():
9696
from django.conf import settings
97-
if 'south' in settings.INSTALLED_APPS:
98-
# Handle south.
99-
from django.core import management
100-
101-
try:
102-
# if `south` >= 0.7.1 we can use the test helper
103-
from south.management.commands import patch_for_test_db_setup
104-
except ImportError:
105-
# if `south` < 0.7.1 make sure it's migrations are disabled
106-
management.get_commands()
107-
management._commands['syncdb'] = 'django.core'
108-
else:
109-
# Monkey-patch south.hacks.django_1_0.SkipFlushCommand to load
110-
# initial data.
111-
# Ref: http://south.aeracode.org/ticket/1395#comment:3
112-
import south.hacks.django_1_0
113-
from django.core.management.commands.flush import (
114-
Command as FlushCommand)
115-
116-
class SkipFlushCommand(FlushCommand):
117-
def handle_noargs(self, **options):
97+
98+
# NOTE: Django 1.7 does not have `management._commands` anymore, which
99+
# is used by South's `patch_for_test_db_setup` and the code below.
100+
if 'south' not in settings.INSTALLED_APPS or get_django_version() > (1, 7):
101+
return
102+
103+
from django.core import management
104+
105+
try:
106+
# if `south` >= 0.7.1 we can use the test helper
107+
from south.management.commands import patch_for_test_db_setup
108+
except ImportError:
109+
# if `south` < 0.7.1 make sure it's migrations are disabled
110+
management.get_commands()
111+
management._commands['syncdb'] = 'django.core'
112+
else:
113+
# Monkey-patch south.hacks.django_1_0.SkipFlushCommand to load
114+
# initial data.
115+
# Ref: http://south.aeracode.org/ticket/1395#comment:3
116+
import south.hacks.django_1_0
117+
from django.core.management.commands.flush import (
118+
Command as FlushCommand)
119+
120+
class SkipFlushCommand(FlushCommand):
121+
def handle_noargs(self, **options):
122+
# Reinstall the initial_data fixture.
123+
from django.core.management import call_command
124+
# `load_initial_data` got introduces with Django 1.5.
125+
load_initial_data = options.get('load_initial_data', None)
126+
if load_initial_data or load_initial_data is None:
118127
# Reinstall the initial_data fixture.
119-
from django.core.management import call_command
120-
# `load_initial_data` got introduces with Django 1.5.
121-
load_initial_data = options.get('load_initial_data', None)
122-
if load_initial_data or load_initial_data is None:
123-
# Reinstall the initial_data fixture.
124-
call_command('loaddata', 'initial_data', **options)
125-
# no-op to avoid calling flush
126-
return
127-
south.hacks.django_1_0.SkipFlushCommand = SkipFlushCommand
128-
129-
patch_for_test_db_setup()
128+
call_command('loaddata', 'initial_data', **options)
129+
# no-op to avoid calling flush
130+
return
131+
south.hacks.django_1_0.SkipFlushCommand = SkipFlushCommand
132+
133+
patch_for_test_db_setup()
130134

131135

132136
# ############### User visible fixtures ################

0 commit comments

Comments
 (0)