title: Django migration strategy for renaming a model and relationship fields
body: <p>I'm planning to rename several models in an existing Django project where there are many other models that have foreign key relationships to the models I would like to rename. I'm fairly certain this will require multiple migrations, but I'm not sure of the exact procedure.</p>
  
  <p>Let's say I start out with the following models within a Django app called <code>myapp</code>:</p>
  
  <pre><code>class Foo(models.Model):
      name = models.CharField(unique=True, max_length=32)
      description = models.TextField(null=True, blank=True)
  
  
  class AnotherModel(models.Model):
      foo = models.ForeignKey(Foo)
      is_awesome = models.BooleanField()
  
  
  class YetAnotherModel(models.Model):
      foo = models.ForeignKey(Foo)
      is_ridonkulous = models.BooleanField()
  </code></pre>
  
  <p>I want to rename the <code>Foo</code> model because the name doesn't really make sense and is causing confusion in the code, and <code>Bar</code> would make for a much clearer name. </p>
  
  <p>From what I have read in the Django development documentation, I'm assuming the following migration strategy:</p>
  
  <h1>Step 1</h1>
  
  <p>Modify <code>models.py</code>:</p>
  
  <pre><code>class Bar(models.Model):  # &lt;-- changed model name
      name = models.CharField(unique=True, max_length=32)
      description = models.TextField(null=True, blank=True)
  
  
  class AnotherModel(models.Model):
      foo = models.ForeignKey(Bar)  # &lt;-- changed relation, but not field name
      is_awesome = models.BooleanField()
  
  
  class YetAnotherModel(models.Model):
      foo = models.ForeignKey(Bar)  # &lt;-- changed relation, but not field name
      is_ridonkulous = models.BooleanField()
  </code></pre>
  
  <p>Note the <code>AnotherModel</code> field name for <code>foo</code> doesn't change, but the relation is updated to the <code>Bar</code> model. My reasoning is that I shouldn't change too much at once and that if I changed this field name to <code>bar</code> I would risk losing the data in that column.</p>
  
  <h1>Step 2</h1>
  
  <p>Create an empty migration:</p>
  
  <pre><code>python manage.py makemigrations --empty myapp
  </code></pre>
  
  <h1>Step 3</h1>
  
  <p>Edit the <code>Migration</code> class in the migration file created in step 2 to add the <code>RenameModel</code> operation to the operations list:</p>
  
  <pre><code>class Migration(migrations.Migration):
  
      dependencies = [
          ('myapp', '0001_initial'),
      ]
  
      operations = [
          migrations.RenameModel('Foo', 'Bar')
      ]
  </code></pre>
  
  <h1>Step 4</h1>
  
  <p>Apply the migration:</p>
  
  <pre><code>python manage.py migrate
  </code></pre>
  
  <h1>Step 5</h1>
  
  <p>Edit the related field names in <code>models.py</code>:</p>
  
  <pre><code>class Bar(models.Model):
      name = models.CharField(unique=True, max_length=32)
      description = models.TextField(null=True, blank=True)
  
  
  class AnotherModel(models.Model):
      bar = models.ForeignKey(Bar)  # &lt;-- changed field name
      is_awesome = models.BooleanField()
  
  
  class YetAnotherModel(models.Model):
      bar = models.ForeignKey(Bar)  # &lt;-- changed field name
      is_ridonkulous = models.BooleanField()
  </code></pre>
  
  <h1>Step 6</h1>
  
  <p>Create another empty migration:</p>
  
  <pre><code>python manage.py makemigrations --empty myapp
  </code></pre>
  
  <h1>Step 7</h1>
  
  <p>Edit the <code>Migration</code> class in the migration file created in step 6 to add the <code>RenameField</code> operation(s) for any related field names to the operations list:</p>
  
  <pre><code>class Migration(migrations.Migration):
  
      dependencies = [
          ('myapp', '0002_rename_fields'),  # &lt;-- is this okay?
      ]
  
      operations = [
          migrations.RenameField('AnotherModel', 'foo', 'bar'),
          migrations.RenameField('YetAnotherModel', 'foo', 'bar')
      ]
  </code></pre>
  
  <h1>Step 8</h1>
  
  <p>Apply the 2nd migration:</p>
  
  <pre><code>python manage.py migrate
  </code></pre>
  
  <hr/>
  
  <p>Aside from updating the rest of the code (views, forms, etc.) to reflect the new variable names, is this basically how the new migration functionality would work?</p>
  
  <p>Also, this seems like a lot of steps. Can the migration operations be condensed in some way?</p>
  
  <p>Thanks!</p>