2012-01-13 54 views
0

我面對這個錯誤在Django:「海峽」對象沒有屬性,當我取消admin.autodiscover「_meta」的錯誤來了()

AttributeError at/
'str' object has no attribute '_meta' 
Request Method: GET 
Request URL: http://localhost:8000/ 
Django Version: 1.3 
Exception Type: AttributeError 
Exception Value:  
'str' object has no attribute '_meta' 
Exception Location: C:\Python27\lib\site-packages\django\contrib\admin\sites.py in   register, line 80 
Python Executable: C:\Python27\python.exe 
Python Version: 2.7.2 
Python Path: ['D:\\programming\\django_projects\\ecomstore', 
'C:\\Python27\\lib\\site-packages\\setuptools-0.6c9-py2.7.egg', 
'C:\\Python27\\lib\\site-packages\\pymysql-0.3-py2.6.egg', 
'C:\\Windows\\system32\\python27.zip', 
'C:\\Python27\\DLLs', 
'C:\\Python27\\lib', 
'C:\\Python27\\lib\\plat-win', 
'C:\\Python27\\lib\\lib-tk', 
'C:\\Python27', 
'C:\\Python27\\lib\\site-packages', 
'C:\\Python27\\lib\\site-packages\\PIL'] 
Server time: Fri, 13 Jan 2012 16:44:18 +0500 

如果我註釋掉admin.autodiscover()不會發生這種錯誤,我也用django中的ModelForm,所以是因爲ModelForm?以下是ModelForm代碼:

from django import forms 
from catalog.models import Product 

class ProductAdminForm(forms.ModelForm): 
    class Meta: 
    model=Product 
    def clean_price(self): 
     if self.cleaned_data['price']<=0: 
      raise forms.ValidationError('Price must be greater than zero') 
     return self.cleaned_data['price'] 

所以,如果有人瞭解問題,那麼請告訴。

以下是回溯,粘貼:

Environment: 


Request Method: GET 
Request URL: http://localhost:8000/ 

Django Version: 1.3 
Python Version: 2.7.2 
Installed Applications: 
['django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'django.contrib.admin', 
    'ecomstore.catalog'] 
Installed Middleware: 
('django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware') 


Traceback: 
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response 
101.        request.path_info) 
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve 
250.    for pattern in self.url_patterns: 
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in _get_url_patterns 
279.   patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in _get_urlconf_module 
274.    self._urlconf_module = import_module(self.urlconf_name) 
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module 
35.  __import__(name) 
File "D:\programming\django_projects\ecomstore\..\ecomstore\urls.py" in <module> 
5. admin.autodiscover() 
File "C:\Python27\lib\site-packages\django\contrib\admin\__init__.py" in autodiscover 
26.    import_module('%s.admin' % app) 
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module 
35.  __import__(name) 
    File "D:\programming\django_projects\ecomstore\..\ecomstore\catalog\admin.py" in <module> 
19. admin.site.register('Product', 'ProductAdmin') 
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in register 
80.    if model._meta.abstract: 

Exception Type: AttributeError at/
Exception Value: 'str' object has no attribute '_meta' 
+1

完整的回溯將非常有幫助。在某處,你用一個字符串而不是一個類對象作爲第一個參數來調用admin.site.register(),但是我們不能告訴沒有TB的地方。 – AdamKG 2012-01-13 12:05:41

+0

此外,ProductAdminForm中的縮進會被搞亂,因爲粘貼clean_price是Meta子類的一種方法,而不是ProductAdminForm,但這可能只是複製/粘貼問題,並且可能不會導致AttributeError。 – AdamKG 2012-01-13 12:05:53

+0

@AdamKG:這只是複製粘貼問題和格式化在stackoverflow編輯器,也追溯現在增加 – Hafiz 2012-01-13 16:49:06

回答

6

的問題是這一行:

admin.site.register('Product', 'ProductAdmin') 

你必須在類對象,而不是字符串傳遞。這應該工作:

admin.site.register(Product, ProductAdmin) 
相關問題