当我取消注释admin.autodiscover()时,'str'对象没有属性'_meta'错误

时间:2012-01-13 11:54:51

标签: django django-admin django-forms django-errors

我在django中面临这个错误:

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 个答案:

答案 0 :(得分:6)

问题是这一行:

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

您必须传入类对象,而不是字符串。这应该有效:

admin.site.register(Product, ProductAdmin)