且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Django未检测到的使用自定义SQL添加的模型字段

更新时间:2021-11-14 23:07:59

您可以使用 RunSQL 来创建具有正确排序规则的字段。调用RunSQL时,使用 state_operations 参数提供状态更改,以使您的SQL代码保持Django最新。

You can use RunSQL to create the field with the proper collation. When calling RunSQL use the state_operations argument to supply the state changes your SQL code made to keep Django up-to-date.

以下是Django文档的示例:

Here's an example from Django's documentation:

from django.db import migrations

class Migration(migrations.Migration):

    operations = [
        migrations.RunSQL(
            "ALTER TABLE musician ADD COLUMN name varchar(255) NOT NULL;",
            state_operations=[
                migrations.AddField(
                    'musician',
                    'name',
                    models.CharField(max_length=255),
                ),
            ],
        ),
    ]