我跟随Mozilla的Django教程,目前在this页上
我试图让ModelAdmin
模型的Book
调用ModelAdmin
的list_display
中Book
模型的函数。这是为了在列表显示中提供类型列表
我在admin.py
文件中有以下代码:
class BookAdmin(admin.ModelAdmin):
list_display('title', 'author', 'display_genre')
在models.py
文件中:
class Book(models.Model):
# ...
def display_genre(self):
return ', '.join(genre.name for genre in self.genre.all()[:3])
display_genre.short_description = 'Genre'
我相信这正是教程要求我添加到每个文件中的内容
以下是django在我尝试调用python manage.py makemigrations
时告诉我的:
SystemCheckError: System check identified some issues:
ERRORS:
<class 'catalog.admin.BookAdmin'>: (admin.E108) The value of 'list_display[2]' refers to 'display_genre', which is not a callable, an attribute of 'BookAdmin', or an attribute or method on 'catalog.Book'.
我不确定我做错了什么
对于任何读到这篇文章的人,我已经让它工作了。以下是方法:
注释掉
admin.py
运行
makemigrations
和migrate
取消对
admin.py
运行
makemigrations
和migrate
现在没有得到错误
也许问题是django在数据库根据
models.py
更新之前正在查看admin.py
文件,因此display_genre
函数还不存在相关问题 更多 >
编程相关推荐