如何在Django1.4中标记不推荐的字段?

2024-04-23 20:29:54 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我不想马上删除django1.4模型中的不推荐字段,那么最好的方法是什么?在


Tags: 方法模型
1条回答
网友
1楼 · 发布于 2024-04-23 20:29:54

抱歉,我还不能评论。在

如果您不想有任何花哨的东西,您可以简单地比较Python版本并发出警告。在

import django
print django.VERSION
>> (1, 8, 5, 'final', 0)
if django.VERSION[1] < 4:
    print "[DEPRECATION WARNING]"

或者你可以做最好的方法:去一个受欢迎的软件包,看看他们是怎么做到的。例如,在Django CMS中:

cms/exceptions.pyhttps://github.com/divio/django-cms/blob/develop/cms/exceptions.py

^{pr2}$

cms/utils/check.pyhttps://github.com/divio/django-cms/blob/develop/cms/utils/check.py

@define_check
def check_deprecated_settings(output):
    with output.section("Deprecated settings") as section:
        found = False
        for deprecated in ['CMS_FLAT_URLS', 'CMS_MODERATOR']:
            if hasattr(settings, deprecated):
                section.warn("Deprecated setting %s found. This setting is no longer in use and can be removed" % deprecated)
                found = True
        if not found:
            section.skip("No deprecated settings found")

相关问题 更多 >