+ if fields_to_add:
+ # First, do the added fields
+ for model, field_name, field in fields_to_add:
+ field_definition = generate_field_definition(model, field)
+
+ if isinstance(field, models.ManyToManyField):
+ # Make a mock model for each side
+ mock_model = "\n".join([
+ create_mock_model(model, " "),
+ create_mock_model(field.rel.to, " ")
+ ])
+ # And a field defn, that's actually a table creation
+ forwards += '''
+ # Mock Model
+%s
+ # Adding ManyToManyField '%s.%s'
+ db.create_table('%s', (
+ ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
+ ('%s', models.ForeignKey(%s, null=False)),
+ ('%s', models.ForeignKey(%s, null=False))
+ )) ''' % (
+ mock_model,
+ model._meta.object_name,
+ field.name,
+ field.m2m_db_table(),
+ field.m2m_column_name()[:-3], # strip off the '_id' at the end
+ model._meta.object_name,
+ field.m2m_reverse_name()[:-3], # strip off the '_id' at the ned
+ field.rel.to._meta.object_name
+ )
+ backwards += '''
+ # Dropping ManyToManyField '%s.%s'
+ db.drop_table('%s')''' % (
+ model._meta.object_name,
+ field.name,
+ field.m2m_db_table()
+ )
+ continue
+ elif field.rel: # ForeignKey, etc.
+ mock_model = create_mock_model(field.rel.to, " ")
+ field_definition = related_field_definition(field, field_definition)
+ else:
+ mock_model = None
+
+ # If we can't get it (inspect madness?) then insert placeholder
+ if not field_definition:
+ print "Warning: Could not generate field definition for %s.%s, manual editing of migration required." % \
+ (model._meta.object_name, field.name)
+ field_definition = '<<< REPLACE THIS WITH FIELD DEFINITION FOR %s.%s >>>' % (model._meta.object_name, f.name)
+
+ if mock_model:
+ forwards += '''
+ # Mock model
+%s
+ ''' % (mock_model)
+
+ forwards += '''
+ # Adding field '%s.%s'
+ db.add_column(%r, %r, %s)
+ ''' % (
+ model._meta.object_name,
+ field.name,
+ model._meta.db_table,
+ field.name,
+ field_definition,
+ )
+ backwards += '''
+ # Deleting field '%s.%s'
+ db.delete_column(%r, %r)
+ ''' % (
+ model._meta.object_name,
+ field.name,
+ model._meta.db_table,
+ field.column,
+ )
+