隨著Django 開發人員的進步,重建專案結構背景
Django 1.7 及更高版本的解決方案
<code class="python">python manage.py makemigrations old_app --empty</code>
<code class="python">class Migration(migrations.Migration): dependencies = [] database_operations = [ migrations.RenameModel('TheModel', 'newapp_themodel') ] state_operations = [ migrations.DeleteModel('TheModel') ] operations = [ migrations.SeparateDatabaseAndState( database_operations=database_operations, state_operations=state_operations) ]</code>
<code class="python">python manage.py makemigrations new_app</code>將模型複製到新應用程式的model.py。
<code class="python">class Migration(migrations.Migration): dependencies = [ ('old_app', 'above_migration') ] state_operations = [ migrations.CreateModel( name='TheModel', fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ], options={ 'db_table': 'newapp_themodel', }, bases=(models.Model,), ) ] operations = [ migrations.SeparateDatabaseAndState(state_operations=state_operations) ]</code>在遷移檔案中,將CreateModel 操作包裝在SeparateDatabaseAndState 作業中,以防止表重新建立:按照以下步驟,您可以成功遷移Django 1.7 及更高版本中的應用程式之間的模型,確保乾淨且可維護的專案結構。
以上是如何在 Django 1.7 中的 Django 應用程式之間遷移模型?的詳細內容。更多資訊請關注PHP中文網其他相關文章!