Commits

Anonymous committed 61a107f

added support for startmigration to generate correct field definitions for fields defined in a parent model (abstract or not)
updated syncdb to work with the new functions in south.migration
fixed a bug where default field values that are strings weren't getting properly quoted in the sql statement

Comments (0)

Files changed (3)

         # if the field is "NOT NULL" and a default value is provided, create the column with it
         # this allows the addition of a NOT NULL field to a table with existing rows
         if not field.null and field.has_default():
+            default = field.get_default()
+            if isinstance(default, basestring):
+                default = "'%s'" % default
             sql += " DEFAULT %s"
-            sqlparams = (field.get_default())
+            sqlparams = (default)
         
         if field.rel:
             self.add_deferred_sql(

management/commands/startmigration.py

             # Touch the init py file
             open(os.path.join(migrations_dir, "__init__.py"), "w").close()
         # See what filename is next in line. We assume they use numbers.
-        migrations = migration.get_migration_names('.'.join(app_module_path))
+        migrations = migration.get_migration_names(app)
         highest_number = 0
         for migration_name in migrations:
             try:
                 for f in model._meta.local_fields:
                     # look up the field definition to see how this was created
                     field_definition = generate_field_definition(model, f)
-                    
-                    if isinstance(f, models.ForeignKey):
-                        mock_models.append(create_mock_model(f.rel.to))
-                        field_definition = related_field_definition(f, field_definition)
+                    if field_definition:
+                        
+                        if isinstance(f, models.ForeignKey):
+                            mock_models.append(create_mock_model(f.rel.to))
+                            field_definition = related_field_definition(f, field_definition)
+                            
+                    else:
+                        print "Warning: Could not generate field definition for %s.%s, manual editing of migration required." % \
+                                (model._meta.object_name, f.name)
+                                
+                        field_definition = '<<< REPLACE THIS WITH FIELD DEFINITION FOR %s.%s >>>' % (model._meta.object_name, f.name)
                                                 
                     fields.append((f.name, field_definition))
                     
             if test_field(' '.join(field_pieces)):
                 return strip_comments(' '.join(field_pieces))
     
-    # TODO:
-    # If field definition isn't found, try looking in models parents.
-    # This should most likely work with just a recursive call to generate_field_definition
-    # supplying the models parents and the current field
-    
     # the 'id' field never gets defined, so return what django does by default
     # django.db.models.options::_prepare
     if field.name == 'id' and field.__class__ == models.AutoField:
         return "models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)"
     
-    print "Warning: Could not generate field definition for %s.%s, manual editing of migration required." % \
-        (model._meta.object_name, field.name)
-        
-    return '<<< REPLACE THIS WITH FIELD DEFINITION FOR %s.%s >>>' % (model._meta.object_name, field.name)
+    # search this classes parents
+    for base in model.__bases__:
+        # we don't want to scan the django base model
+        if base == models.Model:
+            continue
+            
+        field_definition = generate_field_definition(base, field)
+        if field_definition:
+            return field_definition
+            
+    return None
     
 def replace_model_string(field_definition, search_string, model_name):
     # wrap 'search_string' in both ' and " chars when searching

management/commands/syncdb.py

         apps_migrated = []
         for app in models.get_apps():
             app_name = get_app_name(app)
-            migrations = migration.get_migrations(app)
+            migrations = migration.get_migration_package(app)
             if migrations is None:
                 apps_needing_sync.append(app_name)
             else:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.