DJango迁移类

2024-04-18 01:59:53 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个名为students的应用程序,我的应用程序中有一个多类(Class1, Class2,...)型号.py. 当我运行python manage.py migrate时,它将在我的数据库中创建一个新表(students\u class1,students\u class2,…)。你知道吗

现在,我的问题是,迁移应用程序时是否可以跳过特定的类?就像我不想创建一个名为students_class2的表一样

谢谢你!你知道吗


Tags: py数据库应用程序managemigrate型号class1students
1条回答
网友
1楼 · 发布于 2024-04-18 01:59:53

您可能正在模型的Meta类中查找^{}。你知道吗

根据文件:

If False, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means. This is the only difference when managed=False. All other aspects of model handling are exactly the same as normal.

如果已经创建了迁移,请将'managed': False添加到^{}的选项中:

from django.db.migrations import Migration as DjangoMigration, CreateModel


class Migration(DjangoMigration):
    operations = [
        CreateModel(
            name='ModelName',
            fields=[
                # Model's fields.
            ],
            options={
                # Other options.
                'managed': False
            }
        )
    ]

另外,考虑使用^{}来选择适当的表名。你知道吗

相关问题 更多 >