Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress the deprecation warnings in Django?

Every time I'm using the django-admin command — even on TAB–completion — it throws a RemovedInDjango19Warning (and a lot more if I use the test command). How can I suppress those warnings?

I'm using Django 1.8 with Python 3.4 (in a virtual environment). As far as I can tell, all those warnings come from libraries not from my code.

Examples

Here are some examples:

  • …/lib/python3.4/importlib/_bootstrap.py:321: RemovedInDjango19Warning: django.contrib.contenttypes.generic is deprecated and will be removed in Django 1.9. Its contents have been moved to the fields, forms, and admin submodules of django.contrib.contenttypes. return f(*args, **kwds)

  • …/lib/python3.4/site-packages/django/contrib/admin/util.py:7: RemovedInDjango19Warning: The django.contrib.admin.util module has been renamed. Use django.contrib.admin.utils instead. "Use django.contrib.admin.utils instead.", RemovedInDjango19Warning)

  • …/lib/python3.4/site-packages/django/templatetags/future.py:25: RemovedInDjango19Warning: Loading the ``url`` tag from the ``future`` library is deprecated and will be removed in Django 1.9. Use the default ``url`` tag instead. RemovedInDjango19Warning)

Update

Since Django version 1.11 (release notes) deprecating warnings are no longer loud by default. So I guess this won't be an issue anymore, since 1.11 is the last version to support Python 2 and also features long-term support.

like image 653
Brutus Avatar asked Apr 10 '15 12:04

Brutus


People also ask

How do I turn off deprecated warnings?

When nothing else works: $ pip install shutup . Then at the top of the code import shutup;shutup. please() . This will disable all warnings.

How do I turn off Pytest warnings?

Disabling warnings summary Although not recommended, you can use the --disable-warnings command-line option to suppress the warning summary entirely from the test run output.

How do you ignore deprecation warnings in flutter?

Follow the steps at https://flutter.dev/go/android-project-migration to migrate your project. You may also pass the --ignore-deprecation flag to ignore this check and continue with the deprecated v1 embedding. However, the v1 Android embedding will be removed in future versions of Flutter.


1 Answers

Adding a logging filter to settings.py can suppress these console warnings (at least for manage.py commands in Django 1.7, Python 3.4).

A filter can selectively suppress warnings. The following code creates a new "suppress_deprecated" filter for the console and appends it to the default logging filters. Add this block to settings.py to configure the LOGGING variable:

import logging, copy from django.utils.log import DEFAULT_LOGGING  LOGGING = copy.deepcopy(DEFAULT_LOGGING) LOGGING['filters']['suppress_deprecated'] = {     '()': 'mysite.settings.SuppressDeprecated'   } LOGGING['handlers']['console']['filters'].append('suppress_deprecated')  class SuppressDeprecated(logging.Filter):     def filter(self, record):         WARNINGS_TO_SUPPRESS = [             'RemovedInDjango18Warning',             'RemovedInDjango19Warning'         ]         # Return false to suppress message.         return not any([warn in record.getMessage() for warn in WARNINGS_TO_SUPPRESS]) 

The 'mysite.settings.SuppressDeprecated' string needs to change if the root website module (or filter location and/or name) is different.

like image 52
Fred Schleifer Avatar answered Sep 17 '22 16:09

Fred Schleifer