我正在关注Mozilla的Django教程,该教程当前位于this页面上。
我正在尝试为ModelAdmin
模型获得Book
,以在Book
的{{1}}中调用list_display
模型的函数。这是为了在列表显示中提供流派列表。
我在ModelAdmin
文件中有以下代码:
admin.py
在class BookAdmin(admin.ModelAdmin):
list_display('title', 'author', 'display_genre')
文件中:
models.py
我相信这正是本教程要我添加到每个文件中的内容。
这是django尝试呼叫class Book(models.Model):
# ...
def display_genre(self):
return ', '.join(genre.name for genre in self.genre.all()[:3])
display_genre.short_description = 'Genre'
时告诉我的内容:
python manage.py makemigrations
我不确定自己做错了什么。
答案 0 :(得分:0)
对于阅读此书的任何人-我都可以使用。方法如下:
注释list_display
中的admin.py
行
运行makemigrations
和migrate
取消注释list_display
中的admin.py
行
运行makemigrations
和migrate
现在没有收到错误。
也许问题在于django在基于admin.py
更新数据库之前正在查看models.py
文件,因此display_genre
函数还不存在吗?