Django为什么要为代理模型创建迁移文件? [英] Why does Django create migration files for proxy models?

查看:185
本文介绍了Django为什么要为代理模型创建迁移文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建了代理模型,对于 manage.pymakemakemigrations 使用 migrations.CreateModel 操作创建一个新的迁移文件感到惊讶。

I just created a proxy model and was surprised that manage.py makemigrations creates a new migration file with a migrations.CreateModel operation.

代理模型不会创建新的数据库表,它只是同一数据集的不同python接口,实际上是 manage.py sqlmigrate my_app_label 0042 不返回任何内容。

A proxy model does not create a new database table, it's just a different python interface to the same dataset and indeed manage.py sqlmigrate my_app_label 0042 returns nothing.

我认为它可能用于创建代理模型 ContentType ,但这些都是创建的

I thought that it might be used to create the proxy model ContentType but those are created on demand if they don't exist.

它是否用于触发代理模型权限的创建?关于代理模型权限,有一个 6岁的打开错误,所以我不确定这部分如何应该可以使用...

Is it used to trigger the creation of the proxy model permissions? There's a 6 year old open bug on proxy model permissions so I'm not really sure how this part is supposed to work now...

它使用 Django 1.8 进行了测试。

编辑:为澄清起见, Django 创建的迁移不会对新的代理模型起作用,所以我们不会想要 Django 如果没有用,首先不要创建迁移吗?

Edit: to clarify, Django creates a migration that does nothing for new proxy models so wouldn't we want Django to not create the migration in the first place if it's of no use?

是否有用例?

推荐答案

啊,但是如果您在编辑器中打开迁移,您会发​​现这实际上是一个空的迁移!这是一个示例

Ah, but if you open the migration in your editor, you will find that it's actually an empty migration! Here is an example

class Migration(migrations.Migration):
    dependencies = [
        ('stackoverflow', '0009_auto_20160622_1507'),
    ]

    operations = [
        migrations.CreateModel(
            name='MyArticle',
            fields=[
            ],
            options={
                'proxy': True,
            },
            bases=('stackoverflow.article',),
        ),
    ]

如果运行。/manage.py sqlmigrate myapp 0010 (这是与我上面的迁移相对应的数字),您得到的是下一行的内容(什么都没有)。

And if you run ./manage.py sqlmigrate myapp 0010 (which is the number that corresponds to my migration above), what you get is what's on the next line (nothing).

这是因为迁移的字段部分为空,并且选项包含 proxy = True 。此设置可防止此迁移执行任何 SQL ,并且原始表保持不变。

This is because the fields section of the migration is empty and option includes proxy = True. This setting prevents any SQL from being executed for this migration, and the original table is left untouched.

所以您可能会问,为什么 Django 麻烦创建空迁移?那是因为在将来的迁移中,代理模型可能会被其他模型引用。

So you may ask, why does Django bother to create an empty migration? That's because the proxy model may be referred to by another model in a future migration.

这篇关于Django为什么要为代理模型创建迁移文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆