Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14305 -- Switched inspectdb to create unmanaged models.

Thanks Ian Kelly for the report and initial patch.
  • Loading branch information...
commit 08dc90bccf7c4ffa8b04064d74b54c1150af5ff9 1 parent c9c40bc
Ramiro Morales authored February 02, 2013
6  django/core/management/commands/inspectdb.py
@@ -40,8 +40,9 @@ def handle_inspection(self, options):
40 40
         cursor = connection.cursor()
41 41
         yield "# This is an auto-generated Django model module."
42 42
         yield "# You'll have to do the following manually to clean this up:"
43  
-        yield "#     * Rearrange models' order"
44  
-        yield "#     * Make sure each model has one field with primary_key=True"
  43
+        yield "#   * Rearrange models' order"
  44
+        yield "#   * Make sure each model has one field with primary_key=True"
  45
+        yield "#   * Remove `managed = False` lines for those models you wish to give write DB access"
45 46
         yield "# Feel free to rename the models, but don't rename db_table values or field names."
46 47
         yield "#"
47 48
         yield "# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'"
@@ -224,5 +225,6 @@ def get_meta(self, table_name):
224 225
         to the given database table name.
225 226
         """
226 227
         return ["    class Meta:",
  228
+                "        managed = False",
227 229
                 "        db_table = '%s'" % table_name,
228 230
                 ""]
29  docs/howto/legacy-databases.txt
@@ -49,6 +49,35 @@ Once you've cleaned up your models, name the file ``models.py`` and put it in
49 49
 the Python package that holds your app. Then add the app to your
50 50
 :setting:`INSTALLED_APPS` setting.
51 51
 
  52
+If your plan is that your Django application(s) modify data (i.e. edit, remove
  53
+records and create new ones) in the existing database tables corresponding to
  54
+any of the introspected models then one of the manual review and edit steps
  55
+you need to perform on the resulting ``models.py`` file is to change the
  56
+Python declaration of each one of these models to specify it is a
  57
+:attr:`managed <django.db.models.Options.managed>` one. For example, consider
  58
+this generated model definition:
  59
+
  60
+.. parsed-literal::
  61
+
  62
+      class Person(models.Model):
  63
+          id = models.IntegerField(primary_key=True)
  64
+          first_name = models.ChaField(max_length=70)
  65
+          class Meta:
  66
+	       **managed = False**
  67
+	       db_table = 'CENSUS_PERSONS'
  68
+
  69
+If you wanted to modify existing data on your ``CENSUS_PERSONS`` SQL table
  70
+with Django you'd need to change the ``managed`` option highlighted above to
  71
+``True`` (or simply remove it to let it because ``True`` is its default value).
  72
+
  73
+This servers as an explicit opt-in to give your nascent Django project write
  74
+access to your precious data on a model by model basis.
  75
+
  76
+.. versionchanged:: 1.6
  77
+
  78
+The behavior by which introspected models are created as unmanaged ones is new
  79
+in Django 1.6.
  80
+
52 81
 Install the core Django tables
53 82
 ==============================
54 83
 
15  docs/ref/django-admin.txt
@@ -288,9 +288,24 @@ needed.
288 288
 ``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection
289 289
 only works in PostgreSQL and with certain types of MySQL tables.
290 290
 
  291
+If your plan is that your Django application(s) modify data (i.e. edit, remove
  292
+records and create new ones) in the existing database tables corresponding to
  293
+any of the introspected models then one of the manual review and edit steps
  294
+you need to perform on the resulting ``models.py`` file is to change the
  295
+Python declaration of each one of these models to specify it is a
  296
+:attr:`managed <django.db.models.Options.managed>` one.
  297
+
  298
+This servers as an explicit opt-in to give your nascent Django project write
  299
+access to your precious data on a model by model basis.
  300
+
291 301
 The :djadminopt:`--database` option may be used to specify the
292 302
 database to introspect.
293 303
 
  304
+.. versionchanged:: 1.6
  305
+
  306
+The behavior by which introspected models are created as unmanaged ones is new
  307
+in Django 1.6.
  308
+
294 309
 loaddata <fixture fixture ...>
295 310
 ------------------------------
296 311
 
10  tests/regressiontests/inspectdb/tests.py
@@ -140,3 +140,13 @@ def test_special_column_name_introspection(self):
140 140
         self.assertIn("field_field_0 = models.IntegerField(db_column='%s__')" % base_name, output)
141 141
         self.assertIn("field_field_1 = models.IntegerField(db_column='__field')", output)
142 142
         self.assertIn("prc_x = models.IntegerField(db_column='prc(%) x')", output)
  143
+
  144
+    def test_managed_models(self):
  145
+        """Test that by default the command generates models with `Meta.managed = False` (#14305)"""
  146
+        out = StringIO()
  147
+        call_command('inspectdb',
  148
+                     table_name_filter=lambda tn:tn.startswith('inspectdb_columntypes'),
  149
+                     stdout=out)
  150
+        output = out.getvalue()
  151
+        self.longMessage = False
  152
+        self.assertIn("        managed = False", output, msg='inspectdb should generate unmanaged models.')

0 notes on commit 08dc90b

Please sign in to comment.
Something went wrong with that request. Please try again.