7

我是 Django 新手,我正在尝试为我的模型自定义管理界面,但在尝试添加 ModelAdmin 对象时出现错误。我的代码和错误包含在下面。如果我从 register 语句中取出 BlogAdmin 对象,我不会收到任何错误并且站点加载正常。

谢谢你的帮助!

class Blog(models.Model):
    name = models.CharField(max_length=300)
    url = models.URLField()

class BlogAdmin(admin.ModelAdmin):
    list_display = ['name', 'url']
    search_fields = ["name"]

admin.site.register([Blog,BlogAdmin])

我收到以下错误:

类型对象“BlogAdmin”没有属性“_meta”

Exception Location:     /Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/site-packages/django/contrib/admin/sites.py in register, line 80
Python Executable:  /Users/Bilal/Development/Django/ENV/traveller3/bin/python
Python Version:     2.7.1
Python Path:    

['',
 '/Applications/PyCharm.app/helpers/pydev',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/site-packages/pip-1.0.2-py2.7.egg',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/site-packages/MySQL_python-1.2.3c1-py2.7-macosx-10.7-intel.egg',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/site-packages/scikit_learn-0.9-py2.7-macosx-10.7-intel.egg',
 '/Users/Bilal/Development',
 '/Library/Python/2.7/site-packages/pip-1.0.2-py2.7.egg',
 '/Users/Bilal/Development/scrapyard',
 '/Applications/PyCharm.app/helpers',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python27.zip',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/plat-darwin',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/plat-mac',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/Bilal/Development/Django/ENV/traveller3/Extras/lib/python',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/lib-tk',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/lib-old',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/site-packages',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/site-packages/PIL',
 '/Library/Python/2.7/site-packages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Users/Bilal/Development/Django/ENV/traveller3/lib/python2.7/site-packages/IPython/extensions']
4

1 回答 1

18

当你写:

admin.site.register([Blog, BlogAdmin])

您在 admin 中注册了两个模型:Blog并且BlogAdmin,您必须为它注册ModelModelAdmin,如下所示:

admin.site.register(Blog, BlogAdmin) 
于 2012-04-26T03:01:47.707 回答