如何在Django 1.4中标记已弃用的字段?

时间:2016-03-21 13:33:57

标签: python django django-models deprecated

如果我不想立即删除它们,那么在Django 1.4模型中标记已弃用字段的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

对不起,我还不能发表评论。

如果您不想要任何花哨的东西,您可以简单地比较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

# -*- coding: utf-8 -*-

class Deprecated(Exception): pass

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")