如何在数据迁移中访问模型的类级变量?

时间:2015-08-19 18:25:09

标签: django django-models django-1.8

这是我的模特。

where ... in ...

这是我的数据迁移:

Poll(models.Model):
   title = models.CharField(max_length=1024)
   MY_VAR = ['my_class_level_attribute'] # I want to access this

def my_func(apps, schema_editor): Poll = apps.get_model('my_app', 'Poll') print Poll.MY_VAR class Migration(migrations.Migration): dependencies = [ ('webmerge', '0012_previous_migration'), ] operations = [ migrations.RunPython(my_func) ] 给出属性错误。我认为问题可能在于print Poll.MY_VAR在数据迁移中的执行情况,因为以下几行在Django shell中成功:

get_model

2 个答案:

答案 0 :(得分:1)

您应该可以导入模型

from my_app.models import Poll

如果您这样做,则不应删除Poll模型或MY_VAR属性,否则您的迁移将停止工作。

答案 1 :(得分:0)

我认为您无法在迁移中访问模型方法。 我在这里找到了答案How to call a static methods on a django model class during a south migration

相关问题