Ticket #1438: syncdb-inital-sql-data-2.diff
File syncdb-inital-sql-data-2.diff, 2.8 KB (added by , 19 years ago) |
---|
-
django/django/core/management.py
328 328 get_sql_reset.help_doc = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s)." 329 329 get_sql_reset.args = APP_ARGS 330 330 331 def get_sql_initial_data(app ):331 def get_sql_initial_data(app, model_list=None): 332 332 "Returns a list of the initial INSERT SQL statements for the given app." 333 333 from django.conf import settings 334 334 from django.db.models import get_models … … 334 334 from django.db.models import get_models 335 335 output = [] 336 336 337 app_models = get_models(app) 337 if model_list is not None: 338 app_models = model_list 339 else: 340 app_models = get_models(app) 338 341 app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) 339 342 340 343 for klass in app_models: … … 413 416 table_list = introspection_module.get_table_list(cursor) 414 417 415 418 pending_references = [] 419 pending_initial_data = [] 416 420 417 421 for app in models.get_apps(): 418 422 model_list = models.get_models(app) 423 new_models = [] 419 424 for model in model_list: 420 425 # Create the model's database table, if it doesn't already exist. 421 426 if model._meta.db_table in table_list: … … 420 425 # Create the model's database table, if it doesn't already exist. 421 426 if model._meta.db_table in table_list: 422 427 continue 428 new_models.append(model) 423 429 field_metadata, table_metadata, references = sql_for_table(model) 424 430 pending_references.extend(references) 425 431 sql = "CREATE TABLE %s (\n %s\n)" % \ … … 428 434 print "Creating table %s" % model._meta.db_table 429 435 cursor.execute(sql) 430 436 437 if len(new_models) > 0: 438 # Remember to add initial sql data for the new modules 439 pending_initial_data.append((app, new_models)) 440 431 441 for model in model_list: 432 442 # Create the many-to-many join table, if it doesn't already exist. 433 443 for f in model._meta.many_to_many: … … 470 480 backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col)) 471 481 cursor.execute(sql) 472 482 483 # Add initial sql data for the newly installed apps and or models 484 if len(pending_initial_data) > 0: 485 for app, model_list in pending_initial_data: 486 for model in model_list: 487 sql_list = get_sql_initial_data(app, [model]) 488 if len(sql_list) > 0: 489 print "Adding initial sql data for %s" % model._meta.db_table 490 for sql in sql_list: 491 cursor.execute(sql) 492 493 473 494 transaction.commit_unless_managed() 474 495 syncdb.args = '' 475 496