Skip model validation when models are known good.

In some situations Django calls model validation when the models are
already known good. This is most visible in tests, which use flush
and loaddata commands. This resulted in around 10% overhead when
running tests under sqlite.
This commit is contained in:
Anssi Kääriäinen 2012-05-01 08:40:04 +03:00
parent 583f1d7425
commit 4fdd378beb
3 changed files with 11 additions and 8 deletions

View File

@ -198,9 +198,9 @@ class BaseCommand(object):
""" """
Try to execute this command, performing model validation if Try to execute this command, performing model validation if
needed (as controlled by the attribute needed (as controlled by the attribute
``self.requires_model_validation``). If the command raises a ``self.requires_model_validation``, except if force-skipped). If the
``CommandError``, intercept it and print it sensibly to command raises a ``CommandError``, intercept it and print it sensibly
stderr. to stderr.
""" """
show_traceback = options.get('traceback', False) show_traceback = options.get('traceback', False)
@ -226,7 +226,7 @@ class BaseCommand(object):
try: try:
self.stdout = options.get('stdout', sys.stdout) self.stdout = options.get('stdout', sys.stdout)
self.stderr = options.get('stderr', sys.stderr) self.stderr = options.get('stderr', sys.stderr)
if self.requires_model_validation: if self.requires_model_validation and not options.get('skip_validation'):
self.validate() self.validate()
output = self.handle(*args, **options) output = self.handle(*args, **options)
if output: if output:

View File

@ -160,4 +160,5 @@ class Command(NoArgsCommand):
# Load initial_data fixtures (unless that has been disabled) # Load initial_data fixtures (unless that has been disabled)
if load_initial_data: if load_initial_data:
from django.core.management import call_command from django.core.management import call_command
call_command('loaddata', 'initial_data', verbosity=verbosity, database=db) call_command('loaddata', 'initial_data', verbosity=verbosity,
database=db, skip_validation=True)

View File

@ -468,13 +468,14 @@ class TransactionTestCase(SimpleTestCase):
else: else:
databases = [DEFAULT_DB_ALIAS] databases = [DEFAULT_DB_ALIAS]
for db in databases: for db in databases:
call_command('flush', verbosity=0, interactive=False, database=db) call_command('flush', verbosity=0, interactive=False, database=db,
skip_validation=True)
if hasattr(self, 'fixtures'): if hasattr(self, 'fixtures'):
# We have to use this slightly awkward syntax due to the fact # We have to use this slightly awkward syntax due to the fact
# that we're using *args and **kwargs together. # that we're using *args and **kwargs together.
call_command('loaddata', *self.fixtures, call_command('loaddata', *self.fixtures,
**{'verbosity': 0, 'database': db}) **{'verbosity': 0, 'database': db, 'skip_validation': True})
def _urlconf_setup(self): def _urlconf_setup(self):
if hasattr(self, 'urls'): if hasattr(self, 'urls'):
@ -826,7 +827,8 @@ class TestCase(TransactionTestCase):
**{ **{
'verbosity': 0, 'verbosity': 0,
'commit': False, 'commit': False,
'database': db 'database': db,
'skip_validation': True,
}) })
def _fixture_teardown(self): def _fixture_teardown(self):