Opened 7 years ago

Last modified 5 years ago

#28752 closed Cleanup/optimization

django.setup() should be idempotent — at Version 2

Reported by: pascal chambon Owned by: nobody
Component: Core (Other) Version: 1.11
Severity: Normal Keywords:
Cc: Aymeric Augustin Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by pascal chambon)

Alas the ticket #27176 dealt only with the "apps.populate" part, but the whole setup() must be protected, else we'll always have weird side effects on duplicate calls.

Here is a testcase showing the reset of the "django" logger level, for example, when calling setup() multiple times.

Depending on the exact LOGGING dict (with disable_existing_loggers etc.), even the shape of the logging tree might be changed.

    def test_duplicated_setup_calls(self):
        import django, logging
        #from django.conf import settings
        #print(settings.LOGGING_CONFIG, settings.LOGGING)
        
        django.setup()
        logging.getLogger('django').setLevel(logging.DEBUG)
        assert logging.getLogger('django').level == logging.DEBUG

        django.setup()
        assert logging.getLogger('django').level == logging.DEBUG  # raises

Change History (2)

comment:1 by Tim Graham, 7 years ago

Resolution: duplicate
Status: newclosed
Summary: Django.setup() should be idempotentdjango.setup() should be idempotent

I believe this is addressed in Django 2.0 by #27176. If not, please reopen with a minimal project that reproduces the problem you're encountering.

comment:2 by pascal chambon, 7 years ago

Description: modified (diff)
Resolution: duplicate
Status: closednew
Note: See TracTickets for help on using tickets.
Back to Top