Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #453 -- 'django-admin install' now does model validation before…

… installation. Lightly refactored django.core.management to add a get_validation_errors() function, which validate() wraps.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3aa1276bff404a32bd54312d2d7bf5cdcfd8f71c 1 parent 2400642
Adrian Holovaty authored September 02, 2005

Showing 1 changed file with 18 additions and 4 deletions. Show diff stats Hide diff stats

  1. 22  django/core/management.py
22  django/core/management.py
@@ -304,13 +304,24 @@ def init():
304 304
 def install(mod):
305 305
     "Executes the equivalent of 'get_sql_all' in the current database."
306 306
     from django.core import db
  307
+    from cStringIO import StringIO
  308
+    mod_name = mod.__name__[mod.__name__.rindex('.')+1:]
  309
+
  310
+    # First, try validating the models.
  311
+    s = StringIO()
  312
+    num_errors = get_validation_errors(s)
  313
+    if num_errors:
  314
+        sys.stderr.write("Error: %s couldn't be installed, because there were errors in your model:\n" % mod_name)
  315
+        s.seek(0)
  316
+        sys.stderr.write(s.read())
  317
+        sys.exit(1)
307 318
     sql_list = get_sql_all(mod)
  319
+
308 320
     try:
309 321
         cursor = db.db.cursor()
310 322
         for sql in sql_list:
311 323
             cursor.execute(sql)
312 324
     except Exception, e:
313  
-        mod_name = mod.__name__[mod.__name__.rindex('.')+1:]
314 325
         sys.stderr.write("""Error: %s couldn't be installed. Possible reasons:
315 326
   * The database isn't running or isn't configured correctly.
316 327
   * At least one of the database tables already exists.
@@ -495,8 +506,8 @@ def add(self, opts, error):
495 506
         self.errors.append((opts, error))
496 507
         self.outfile.write("%s.%s: %s\n" % (opts.app_label, opts.module_name, error))
497 508
 
498  
-def validate(outfile=sys.stdout):
499  
-    "Validates all installed models."
  509
+def get_validation_errors(outfile):
  510
+    "Validates all installed models. Writes errors, if any, to outfile. Returns number of errors."
500 511
     import django.models
501 512
     from django.core import meta
502 513
     e = ModelErrorCollection(outfile)
@@ -543,8 +554,11 @@ def validate(outfile=sys.stdout):
543 554
                     e.add(rel_opts, "At least one field in %s should have core=True, because it's being edited inline by %s.%s." % (rel_opts.object_name, opts.module_name, opts.object_name))
544 555
                 except StopIteration:
545 556
                     pass
  557
+    return len(e.errors)
546 558
 
547  
-    num_errors = len(e.errors)
  559
+def validate(outfile=sys.stdout):
  560
+    "Validates all installed models."
  561
+    num_errors = get_validation_errors(outfile)
548 562
     outfile.write('%s error%s found.\n' % (num_errors, num_errors != 1 and 's' or ''))
549 563
 validate.args = ''
550 564
 

0 notes on commit 3aa1276

Please sign in to comment.
Something went wrong with that request. Please try again.