Commits

Anonymous committed 4ee8287

M2M fields are now supported by startmigration

Comments (0)

Files changed (1)

management/commands/startmigration.py

 from django.core.management.color import no_style
 from django.db import models
 from django.db.models.fields.related import RECURSIVE_RELATIONSHIP_CONSTANT
+from django.contrib.contenttypes.generic import GenericRelation
 from optparse import make_option
 from south import migration
 import sys
         
                 forwards += '''
         # Model '%s'
-        db.create_table("%s", (
+        db.create_table('%s', (
             %s
         ))''' % (
                     model._meta.object_name,
                 )
 
                 backwards += '''
-        db.delete_table("%s")''' % table_name
+        db.delete_table('%s')''' % table_name
+        
                 # Now go through local M2Ms and add extra stuff for them
-        #         for m in model._meta.local_many_to_many:
-        #             forwards += '''
-        # # M2M field '%s'
-        # db.create_table("%s", [
-        #     {"name": "id", "type": "serial", "null": False, "unique": True},
-        #     {"name": "%s", "type": "integer", "null": False, "related_to": ("%s", "%s")},
-        #     {"name": "%s", "type": "integer", "null": False, "related_to": ("%s", "%s")},
-        # ]) ''' % (
-        #                 m.name,
-        #                 m.m2m_db_table(),
-        #                 m.m2m_column_name(),
-        #                 table_name,
-        #                 "id",
-        #                 m.m2m_reverse_name(),
-        #                 m.rel.to._meta.db_table,
-        #                 "id",
-        #         )
-        #         
-        #             backwards += '''
-        # db.delete_table("%s")''' % m.m2m_db_table()
+                for m in model._meta.local_many_to_many:
+                    # ignore generic relations
+                    if isinstance(m, GenericRelation):
+                        continue
+
+                    # if the 'through' option is specified, the table will
+                    # be created through the normal model creation above.
+                    if m.rel.through:
+                        continue
+                        
+                    mock_models = [create_mock_model(model), create_mock_model(m.rel.to)]
+                    
+                    forwards += '''
+        # Mock Models
+        %s
+        
+        # M2M field '%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))
+        )) ''' % (
+                        "\n        ".join(mock_models),
+                        model._meta.object_name,
+                        m.name,
+                        m.m2m_db_table(),
+                        m.m2m_column_name()[:-3], # strip off the '_id' at the end
+                        model._meta.object_name,
+                        m.m2m_reverse_name()[:-3], # strip off the '_id' at the ned
+                        m.rel.to._meta.object_name
+                )
+                
+                    backwards += '''
+        db.delete_table('%s')''' % m.m2m_db_table()
                 
             forwards += '''
         
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.