2

我在我的设置文件中设置 AUTH_PROFILE_MODULE,如下所示:

AUTH_PROFILE_MODULE = 'api.AccountProfile'

API 应用程序被添加到已安装的应用程序中,如下所示:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    #3rd party apps
    'south',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',
    'allauth.socialaccount.providers.twitter',

    #Our apps
    'ourproject.apps.api',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

我运行./manage.py syncdb并且./manage.py migrate没有困难,然后我运行 django 单元测试./manage.py test。第一个失败的测试是 AUTH_PROFILE_MODULE 测试,它位于django.contrib.auth.tests.models.ProfileTestCase.

这个特定的测试一开始就失败了:

def test_site_profile_not_available(self):
    # calling get_profile without AUTH_PROFILE_MODULE set
    if hasattr(settings, 'AUTH_PROFILE_MODULE'):
        del settings.AUTH_PROFILE_MODULE    #<----Fails here

在堆栈的下方,我发现错误发生在最终在包装器上调用的方法中__delattr__django.utils.functional.LazyObjectdelattr

def __delattr__(self, name):
    if name == "_wrapped":
        raise TypeError("can't delete _wrapped.")
    if self._wrapped is empty:
        self._setup()
    delattr(self._wrapped, name)    #<----Fails here

这是追溯:

File "/path/to/python/files/django/contrib/auth/tests/models.py", line 30, in test_site_profile_not_available
    del settings.AUTH_PROFILE_MODULE
File "/path/to/python/files/django/utils/functional.py", line 211, in __delattr__
    delattr(self._wrapped, name)
AttributeError: 'UserSettingsHolder' object has no attribute 'AUTH_PROFILE_MODULE'

所以我挖掘了 django 源代码并添加了一些打印语句以查看发生了什么:

django.contrib.auth.tests.models.ProfileTestCase:

def test_site_profile_not_available(self):
    # calling get_profile without AUTH_PROFILE_MODULE set
    if hasattr(settings, 'AUTH_PROFILE_MODULE'):
        print '*******', type(settings), '********'
        del settings.AUTH_PROFILE_MODULE

Output: ******* <class 'django.conf.LazySettings'> ********

django.utils.functional.LazyObject:

def __delattr__(self, name):
    if name == "_wrapped":
        raise TypeError("can't delete _wrapped.")
    if self._wrapped is empty:
        print 'Here I am. Rock you like a hurricane'
        self._setup()
    print '*************', hasattr(self._wrapped, name)
    print '*************', name
    print '*************', self._wrapped.AUTH_PROFILE_MODULE
    delattr(self._wrapped, name)

Output: ************* True
************* AUTH_PROFILE_MODULE
************* api.AccountProfile

请注意,LazySettings继承自LazyObject并且没有自己的delattr方法。

这是完整的输出./manage.py testhttps ://dl.dropbox.com/u/20560722/TestOutput.txt 。请注意,有问题的测试只运行一次,打印语句只显示一次。

4

1 回答 1

1

这是一个已知的 Django 问题,已在最新的 master (1.5dev) https://code.djangoproject.com/ticket/17966上修复。

于 2012-09-19T19:33:45.250 回答