Skip to content

AppConf subclass settings disappear in second and subsequent uses of override_settings #30

Closed
@mhl

Description

@mhl

(This might be related to #29 but since the symptoms are different I'm creating a new issue.)

If you've added settings using a subclass of AppConf from a Django application, it seems that those settings are available in the first override_settings-wrapped method, but not in subsequent ones.

I created a minimal django project to demonstrate this, which you can clone from here: https://github.com/mhl/django-appconf-test

The AppConf subclass, which is used in the only view, is:

class ExampleExtraConf(AppConf):
    MY_EXTRA_SETTING = 'foo'

... and the two (identical) tests are:

from django.test import TestCase
from django.test.utils import override_settings


class ExampleTests(TestCase):

    @override_settings(MEDIA_ROOT='/var/tmp/')
    def test_once(self):
        response = self.client.get('/')
        self.assertContains(response, 'foo')

    @override_settings(MEDIA_ROOT='/var/tmp/')
    def test_twice(self):
        response = self.client.get('/')
        self.assertContains(response, 'foo')

You'll find that test_once passes, but test_twice fails:

 ./manage.py test
Creating test database for alias 'default'...
.E
======================================================================
ERROR: test_twice (myproject.tests.ExampleTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/mark/.virtualenvs/minimal-django-test-project/local/lib/python2.7/site-packages/django/test/utils.py", line 196, in inner
    return test_func(*args, **kwargs)
  File "/home/mark/minimal-django-test-project/myproject/tests.py", line 14, in test_twice
    response = self.client.get('/')
  File "/home/mark/.virtualenvs/minimal-django-test-project/local/lib/python2.7/site-packages/django/test/client.py", line 500, in get
    **extra)
  File "/home/mark/.virtualenvs/minimal-django-test-project/local/lib/python2.7/site-packages/django/test/client.py", line 303, in get
    return self.generic('GET', path, secure=secure, **r)
  File "/home/mark/.virtualenvs/minimal-django-test-project/local/lib/python2.7/site-packages/django/test/client.py", line 379, in generic
    return self.request(**r)
  File "/home/mark/.virtualenvs/minimal-django-test-project/local/lib/python2.7/site-packages/django/test/client.py", line 466, in request
    six.reraise(*exc_info)
  File "/home/mark/.virtualenvs/minimal-django-test-project/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/mark/minimal-django-test-project/example/views.py", line 7, in homepage
    settings.EXAMPLE_MY_EXTRA_SETTING
  File "/home/mark/.virtualenvs/minimal-django-test-project/local/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in __getattr__
    return getattr(self._wrapped, name)
  File "/home/mark/.virtualenvs/minimal-django-test-project/local/lib/python2.7/site-packages/django/conf/__init__.py", line 160, in __getattr__
    return getattr(self.default_settings, name)
AttributeError: 'Settings' object has no attribute 'EXAMPLE_MY_EXTRA_SETTING'

----------------------------------------------------------------------
Ran 2 tests in 0.045s

FAILED (errors=1)
Destroying test database for alias 'default'...

To help people who find this via Google, we came across this when finding that the django-statici18n statici18n template tag was failing in some tests where override_settings was used, producing a traceback like:

  File "/home/mark/.virtualenvs/ynr-popolo-upstream/local/lib/python2.7/site-packages/django/template/base.py", line 1197, in render
    return func(*resolved_args, **resolved_kwargs)
  File "/home/mark/.virtualenvs/ynr-popolo-upstream/local/lib/python2.7/site-packages/statici18n/templatetags/statici18n.py", line 34, in statici18n
    return static(get_path(locale))
  File "/home/mark/.virtualenvs/ynr-popolo-upstream/local/lib/python2.7/site-packages/statici18n/templatetags/statici18n.py", line 21, in get_path
    return os.path.join(settings.STATICI18N_OUTPUT_DIR,
  File "/home/mark/.virtualenvs/ynr-popolo-upstream/local/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in __getattr__
    return getattr(self._wrapped, name)
  File "/home/mark/.virtualenvs/ynr-popolo-upstream/local/lib/python2.7/site-packages/django/conf/__init__.py", line 160, in __getattr__
    return getattr(self.default_settings, name)
AttributeError: 'Settings' object has no attribute 'STATICI18N_OUTPUT_DIR'

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions