如果字段的选择列表发生更改,请停止Django创建迁移 [英] Stop Django from creating migrations if the list of choices of a field changes
问题描述
我有一个名叫foocore的django核心应用程序。
I have a django core app called "foocore".
有几个可选的pluging类似的应用程序。例如superfoo。
There are several optional pluging-like apps. For example "superfoo".
在我的情况下,每个插件在一个属于foocore的模型CharField中添加了一个新的选择。
In my case every plugin adds a new choice in a model CharField which belongs to "foocore".
如果选择列表发生更改,Django迁移会检测更改。
Django migrations detect changes if the list of choices get changed.
我认为这不是必需的。至少有一个开发人员认为相同:
I think this is not necessary. At least one other developer thinks the same:
https:// code。 djangoproject.com/ticket/22837
class ActivePlugin(models.Model):
plugin_name = models.CharField(max_length=32, choices=get_active_plugins())
获取选项的代码: / p>
The code to get the choices:
class get_active_plugins(object):
def __iter__(self):
for item in ....:
yield item
核心foocore用于多个项目,每个安装都一组不同的插件。 Django尝试创建无用的迁移....
The core "foocore" gets used in several projects and every installation has a different set of plugins. Django tries to create useless migrations ....
有没有办法解决这个问题?
Is there a way to work around this?
推荐答案
有关详细信息,请参阅此错误报告和讨论: https://code.djangoproject.com/ticket/22837
See this bug report and discussion for more info: https://code.djangoproject.com/ticket/22837
提出的解决方案是使用可以作为选择的参数,但是它似乎没有被执行为字段,但仅用于表单。
The proposed solution was to use a callable as the argument for choices, but it appears this has not been executed for fields but for forms only.
如果真的需要动态选择,而不是 ForeignKey
是最好的解决方案。
If you really need dynamic choices than a ForeignKey
is the best solution.
另一种解决方案可以是通过自定义的清洁方法为字段添加要求和/或创建一个自定义窗体。表单字段支持可调用选项
。
An alternative solution can be to add the requirement through a custom clean method for the field and/or creating a custom form. Form fields do support callable choices
.
有关详细信息,请参阅此答案:https://stackoverflow.com/a/33514551/54017
See this answer for more info: https://stackoverflow.com/a/33514551/54017
这篇关于如果字段的选择列表发生更改,请停止Django创建迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!