不能沉默django-cms产生的警告

时间:2015-02-10 04:56:49

标签: python django django-cms

我用djangocms-installer脚本安装了Django-cms,一切正常,只是每次启动服务器时shell都会收到一堆RemovedInDjango18Warning警告,对manage.py做任何事情,甚至做一个manage.py tab-autocomplete(最烦人的)!所以我想我使用warnings模块来沉默警告:

# in manage.py, just after `import os; import sys`:
import warnings
warnings.filterwarnings("ignore")

我想更加具体的沉默,但事实证明即使这个简单的情况也没有做任何事情,警告仍然显示!我做错了什么?!

警告:

/Users/fran/.virtualenvs/dkde2015/lib/python2.7/site-packages/cms/publisher/manager.py:5: RemovedInDjango18Warning: `PublisherManager.get_query_set` method should be renamed `get_queryset`.
  class PublisherManager(models.Manager):

/Users/fran/.virtualenvs/dkde2015/lib/python2.7/site-packages/cms/models/managers.py:15: RemovedInDjango18Warning: `PageManager.get_query_set` method should be renamed `get_queryset`.
  class PageManager(PublisherManager):

/Users/fran/.virtualenvs/dkde2015/lib/python2.7/site-packages/cms/admin/change_list.py:39: RemovedInDjango18Warning: `CMSChangeList.get_query_set` method should be renamed `get_queryset`.
  class CMSChangeList(ChangeList):

/Users/fran/.virtualenvs/dkde2015/lib/python2.7/site-packages/cms/admin/forms.py:340: RemovedInDjango18Warning: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is deprecated - form PagePermissionInlineAdminForm needs updating
  class PagePermissionInlineAdminForm(forms.ModelForm):

/Users/fran/.virtualenvs/dkde2015/lib/python2.7/site-packages/cms/admin/forms.py:442: RemovedInDjango18Warning: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is deprecated - form ViewRestrictionInlineAdminForm needs updating
  class ViewRestrictionInlineAdminForm(PagePermissionInlineAdminForm):

/Users/fran/.virtualenvs/dkde2015/lib/python2.7/site-packages/cms/admin/forms.py:491: RemovedInDjango18Warning: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is deprecated - form PageUserForm needs updating
  class PageUserForm(UserCreationForm, GenericCmsPermissionForm):

/Users/fran/.virtualenvs/dkde2015/lib/python2.7/site-packages/django/forms/widgets.py:143: RemovedInDjango18Warning: `PagePermissionInlineAdmin.queryset` method should be renamed `get_queryset`.
  .__new__(mcs, name, bases, attrs))

/Users/fran/.virtualenvs/dkde2015/lib/python2.7/site-packages/django/forms/widgets.py:143: RemovedInDjango18Warning: `ViewRestrictionInlineAdmin.queryset` method should be renamed `get_queryset`.
  .__new__(mcs, name, bases, attrs))

/Users/fran/.virtualenvs/dkde2015/lib/python2.7/site-packages/django/forms/widgets.py:143: RemovedInDjango18Warning: `PageUserAdmin.queryset` method should be renamed `get_queryset`.
  .__new__(mcs, name, bases, attrs))

3 个答案:

答案 0 :(得分:7)

解决此问题的手术方法是创建一个日志记录过滤器,它只过滤掉明确指定为静音的警告。你知道的:

  

错误不应该默默地传递。
     除非明确地保持沉默。

考虑到这一点,这是我的过滤机械,全部都在settings.py(现在还是):

MY_IGNORED_WARNINGS = {
    'RemovedInDjango18Warning: `PublisherManager.get_query_set`',
    'RemovedInDjango18Warning: `PageManager.get_query_set`',
    'RemovedInDjango18Warning: `CMSChangeList.get_query_set`',
    'form PagePermissionInlineAdminForm needs updating',
    'form ViewRestrictionInlineAdminForm needs updating',
    'form PageUserForm needs updating',
    '/cms/admin/placeholderadmin.py:133: RemovedInDjango18Warning: Options.module_name has been deprecated',
    '/cms/admin/settingsadmin.py:28: RemovedInDjango18Warning: Options.module_name has been deprecated',
    '/cms/admin/pageadmin.py:111: RemovedInDjango18Warning: Options.module_name has been deprecated',
    'RemovedInDjango18Warning: `PagePermissionInlineAdmin.queryset',
    'RemovedInDjango18Warning: `ViewRestrictionInlineAdmin.queryset`',
    'RemovedInDjango18Warning: `PageUserAdmin.queryset`',

}

def filter_djangocms_warnings(record):
    for ignored in MY_IGNORED_WARNINGS:
        if ignored in record.args[0]:
            return False
    return True

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'filters': {
        'ignore_djangocms_warnings': {
            '()': 'django.utils.log.CallbackFilter',
            'callback': filter_djangocms_warnings,
        },
    },
    'loggers': {
        'py.warnings': {
            'handlers': ['console', ],
            'filters': ['ignore_djangocms_warnings', ],
        }
    },
}

我想它可以进一步完善......任何想法?

为什么要这么麻烦?

首先,我可以现在(再次)使用django bash autocomplete,而不是每次按[tab]时都会回复一堆警告。

此外,既然我没有从DjangoCMS发出20多个警告,我实际上可以看到我自己的代码会发出什么警告并修复它。

答案 1 :(得分:3)

这实际上与Django CMS无关,即使您可能会看到Django CMS使用的模块发出的警告。

原因是Django> = 1.5.x会根据此change将所有警告路由到日志记录系统。因此,要在Django中禁用警告,您必须为py.warnings添加记录器(如Python' warning module中所述):

# settings.py
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
         'null': {
           'level': 'DEBUG',
           'class': 'logging.NullHandler',
         }
     },
    'loggers': {
         'py.warnings': {
             'propagate': False,
             'handlers': ['null']
          }
     },
}

答案 2 :(得分:1)

您可以使用-W标记来过滤警告。

python -W ignore manage.py runserver