gpt4 book ai didi

python - Django 迁移中不存在 Mixin 字段

转载 作者:行者123 更新时间:2023-11-30 22:25:31 25 4
gpt4 key购买 nike

假设我有这样的混合:

class Test(object):
...some logic...

class TestMixin(models.Model):
db_field = django database field

test = Test() # not a database field

class Meta:
abstract = True

class Foo(TestMixin, models.Model):
... more db fields ...

我在这里遇到一个奇怪的问题。如果我通过 django shell 检查 Foo,我可以看到两个字段,db_field 和 test

但是如果我创建此迁移:

from __future__ import unicode_literals

from django.db import migrations


def custom_operation(apps, schema_editor):
Foo = apps.get_model('django_app', 'Foo')
stuffs = Foo.objects.all()
for stuff in stuffs:
print stuff.test # this doesnt exist

class Migration(migrations.Migration):

dependencies = [
...
]

operations = [
migrations.RunPython(custom_operation),
]

如果我在 Test() __init__ 添加断点,它会通过 shell 或 Django 被调用,但在运行迁移时不会被调用。

通过迁移使用模型有什么区别?

最佳答案

https://docs.djangoproject.com/en/1.11/topics/migrations/#historical-models

特别是这句话:

Because it’s impossible to serialize arbitrary Python code, these historical models will not have any custom methods that you have defined. They will, however, have the same fields, relationships, managers (limited to those with use_in_migrations = True) and Meta options (also versioned, so they may be different from your current ones).

关于python - Django 迁移中不存在 Mixin 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47509358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com