Anonymous avatar Anonymous committed d701903

Add support in startmigration for mockmodels which refer to models with a primary key field that needs arguments

Comments (0)

Files changed (1)

management/commands/startmigration.py

         sys.exit()
     
     pk_field_args = []
+    pk_field_kwargs = {}
     other_mocks = []
     # If it's a OneToOneField or ForeignKey, take it's first arg
     if model._meta.pk.__class__.__name__ in ["OneToOneField", "ForeignKey"]:
         if model._meta.pk.rel.to == model:
-            pk_field_args += ["self"]
+            pk_field_args += ["'self'"]
         else:
             pk_field_args += [model._meta.pk.rel.to._meta.object_name]
             other_mocks += [model._meta.pk.rel.to]
     
-    return "%s%s%s = db.mock_model(model_name='%s', db_table='%s', db_tablespace='%s', pk_field_name='%s', pk_field_type=models.%s, pk_field_args=[%s])" % \
+    # Perhaps it has a max_length set?
+    if model._meta.pk.max_length:
+        pk_field_kwargs["max_length"] = model._meta.pk.max_length
+    
+    return "%s%s%s = db.mock_model(model_name='%s', db_table='%s', db_tablespace='%s', pk_field_name='%s', pk_field_type=models.%s, pk_field_args=[%s], pk_field_kwargs=%r)" % \
         (
         "\n".join([create_mock_model(m, indent) for m in other_mocks]+[""]),
         indent,
         model._meta.pk.name,
         model._meta.pk.__class__.__name__,
         ", ".join(pk_field_args),
+        pk_field_kwargs,
         )
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.