我必须在两个不同的应用程序(例如app1和app2)中使用django模型,在app1中,我得到了Model1,在app2中,我得到了BaseModel,而Model1就是这样
class Model1(BaseModel):
...
Model1和BaseModel在一个应用程序中,但是我将Model1移到了app2,现在我也想将BaseModel也移到app2。 我的问题是,当我尝试将BaseModel移至app2时,出现此错误:
Cannot resolve bases for [<ModelState: 'app1.model1'>]
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)
in an app with no migrations; see https://docs.djangoproject.com/en/2.1/topics/migrations/#dependencies for more
我的工作很简单:
此方法可用于移动Model1,但是当我尝试移动此基本模型时,出现此错误。
我非常感谢您提供的任何帮助,包括采取其他任何方法来实现将BaseModel移至app1的重构想法。
答案 0 :(得分:1)
Django并未为此提供内置的迁移操作,但是您可以通过将多个迁移操作拼凑在一起来实现此目的。
迁移不仅会更改数据库,而且还会保留所有模型的状态。您必须确保状态与数据库保持同步,这会使事情变得有些复杂。密钥正在使用migrations.SeparateDatabaseAndState
。
假设您具有以下模型定义:
# app1/models.py
from django.db import models
class BaseModel(models.Model):
base_field = models.CharField(max_length=64)
# app2/models.py
from django.db import models
from app1.models import BaseModel
class Model1(BaseModel):
model_field = models.CharField(max_length=64)
您要迁移到此:
# app1/models.py empty
# app2/models.py
from django.db import models
class BaseModel(models.Model):
base_field = models.CharField(max_length=64)
class Model1(BaseModel):
model_field = models.CharField(max_length=64)
您必须创建三个迁移:
app1.BaseModel
的表从app1_basemodel
重命名为app2_basemodel
。这还需要调整basemodel_ptr
列上的外键约束。app2.BaseModel
并使用app2.Model1
作为基础模型重新创建app2.BaseModel
。这些更改仅在迁移状态上完成,并且不影响数据库!app1.BaseModel
。同样,没有数据库更改。这是代码中的样子:
# app1/migrations/0002_rename_basemodel_table.py
from django.db import migrations, models
class Migration(migrations.Migration):
atomic = False
dependencies = [
('app1', '0001_initial'),
]
operations = [
migrations.AlterModelTable(
name='BaseModel',
table='app2_basemodel'
),
]
# app2/migrations/0002_change_basemodel.py
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('app2', '0001_initial'),
('app1', '0002_rename_basemodel_table')
]
state_operations = [
migrations.CreateModel(
name='BaseModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('base_field', models.CharField(max_length=64)),
],
),
migrations.DeleteModel(
name='Model1',
),
migrations.CreateModel(
name='Model1',
fields=[
('basemodel_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='app2.BaseModel')),
],
bases=('app2.basemodel',),
),
]
database_operations = [
]
operations = [
migrations.SeparateDatabaseAndState(
database_operations,
state_operations
)
]
# app1/0003_remove_basemodel.py
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app1', '0002_rename_basemodel_table'),
('app2', '0002_change_basemodel')
]
state_operations = [
migrations.DeleteModel(
name='BaseModel',
),
]
operations = [
migrations.SeparateDatabaseAndState(
database_operations=None,
state_operations=state_operations
)
]
很显然,您必须调整这些迁移以反映您的实际模型。
恐怕如果您还有其他与Model1
有关联的模型,这可能会变得更加复杂。
免责声明::我已经在SQLite和PostgreSQL上对它们进行了测试,但后果自负!在对生产数据运行备份之前,请确保已备份。
之前:
$ python manage.py dbshell
SQLite version 3.19.3 2017-06-27 16:48:08
Enter ".help" for usage hints.
CREATE TABLE IF NOT EXISTS "app1_basemodel" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "base_field" varchar(64) NOT NULL);
CREATE TABLE IF NOT EXISTS "app2_model1" ("basemodel_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "app1_basemodel" ("id") DEFERRABLE INITIALLY DEFERRED, "model_field" varchar(64) NOT NULL);
...
$ python manage.py shell
Python 3.7.0 (default, Aug 22 2018, 15:22:33)
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from app2.models import Model1
>>> Model1.__bases__
(<class 'app1.models.BaseModel'>,)
>>> Model1.objects.create(base_field='a', model_field='A')
<Model1: Model1 object (1)>
>>> Model1.objects.create(base_field='b', model_field='B')
<Model1: Model1 object (2)>
>>>
之后:
sqlite> .schema
...
CREATE TABLE IF NOT EXISTS "app2_basemodel" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "base_field" varchar(64) NOT NULL);
CREATE TABLE IF NOT EXISTS "app2_model1" ("basemodel_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "app2_basemodel" ("id") DEFERRABLE INITIALLY DEFERRED, "model_field" varchar(64) NOT NULL);
...
>>> from app2.models import Model1
>>> Model1.__bases__
(<class 'app2.models.BaseModel'>,)
>>> for obj in Model1.objects.all():
... print(obj.base_field, obj.model_field)
...
a A
b B
或者,您可以查看writing a custom migration operation。
答案 1 :(得分:0)
我试图从状态中删除model1,但是它起作用了 这是我为使其正常工作所做的事情(某些名称与我所写的内容有所不同):
将模型1从状态中删除
state_operations = [migrations.DeleteModel('Model')]
operations =[migrations.SeparateDatabaseAndState(state_operations=state_operations)]
BaseModel的命名表(您需要为SQLite设置atomic = True)
operations = [
migrations.AlterModelTable(
name='basemodel',
table='first_basemodel',
),
]
将模型移至第一个应用程序
def update_contentypes(apps, schema_editor):
ContentType = apps.get_model('contenttypes', 'ContentType')
db_alias = schema_editor.connection.alias
qs = ContentType.objects.using(db_alias).filter(app_label='second', model='basemodel')
qs.update(app_label='first')
def update_contentypes_reverse(apps, schema_editor):
ContentType = apps.get_model('contenttypes', 'ContentType')
db_alias = schema_editor.connection.alias
qs = ContentType.objects.using(db_alias).filter(app_label='first', model='basemodel')
qs.update(app_label='second')
class Migration(migrations.Migration):
dependencies = [
('first', '0002_delete_model_from_state'),
('second', '0002_auto_20181218_0717')
]
state_operenterations = [
migrations.CreateModel(
name='BaseModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)),
],
),
]
database_operations = [
migrations.RunPython(update_contentypes, update_contentypes_reverse),
]
operations = [
migrations.SeparateDatabaseAndState(
state_operations=state_operations,
database_operations=database_operations
),
]
从状态中删除app2中的BaseModel
state_operations = [
migrations.DeleteModel('BaseModel'),
]
operations = [
migrations.SeparateDatabaseAndState(state_operations=state_operations)
]
移动代码并添加回Model1
state_operations = [migrations.CreateModel(
name='Model',
fields=[
('basemodel_ptr', models.OneToOneField(auto_created=True, on_delete=models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='first.BaseModel')),
('title', models.CharField(max_length=200)),
],
bases=('first.basemodel',),),]
operations = [migrations.SeparateDatabaseAndState(state_operations=state_operations)]
此解决方案对我有效,并且没有在SQLite和PSQL上测试过任何数据丢失。