Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't silence warnings that django-cms produces

I installed Django-cms with the djangocms-installer script, and all works fine except that I get a bunch of RemovedInDjango18Warning warnings in the shell every time I start the server, do anything with manage.py, or even do a manage.py tab-autocomplete (most annoying)! So thought I'd silence the warnings, using warnings module:

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

I would like to get more specific with the silencing, but it turns out that even this simple case does not do anything, the warnings are still displayed! What am I doing wrong?!

The warnings:

/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))
like image 326
frnhr Avatar asked Feb 10 '15 04:02

frnhr


Video Answer


2 Answers

The surgical way to solve this is to create a logging filter that will only filter out warnings that are explicitly specified to be silenced. You know:

Errors should never pass silently.
Unless explicitly silenced.


With that in mind, here is my filter machinery, all in settings.py (for now anyways):

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', ],
        }
    },
}

I imagine it can be further refined... any ideas?


Why bother?

For one, I can use now (again) use django bash autocomplete without a bunch of warnings echoed every time I press [tab].

Also, now that I don't have 20+ warnings form DjangoCMS, I can actually see what warnings come from my own code and fix it.

like image 67
frnhr Avatar answered Sep 20 '22 00:09

frnhr


This is actually unrelated to Django CMS, even though you may see the warnings issued by a module that Django CMS uses.

The reason is that Django >= 1.5.x routes all warnings to the logging system as per this change. So to disable warnings in Django you have to add a logger for py.warnings (as documented in Python's 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']
          }
     },
}
like image 20
miraculixx Avatar answered Sep 20 '22 00:09

miraculixx