我正试图通过Practical Django Projects工作.它似乎有点旧,但我已经设法将代码转换到这一点.
在这一点上,本书希望我将models.py更改为:
class SearchKeyword(models.Model)
keyword = models.CharField(maxlength=50, core=True)
page = models.ForeignKey(FlatPage, edit_inline=models.STACKED,
min_num_in_admin=3, num_extra_on_change=1)
我知道现在这已经在admin.py中完成了.所以我的models.py看起来像这样:
from django.db import models
from django.contrib.flatpages.models import FlatPage
class SearchKeyword(models.Model):
keyword = models.CharField(max_length=50)
page = models.ForeignKey(FlatPage)
class Admin:
pass
def __unicode__(self):
return self.keyword
我现在创建的admin.py看起来像这样:
from search.models import SearchKeyword
from django.contrib import admin
from django.contrib.flatpages.models import FlatPage
class SearchKeywordInline(admin.StackedInline):
model = SearchKeyword
extra = 3
class FlatPageAdmin(admin.ModelAdmin):
model = FlatPage
inlines = [SearchKeywordInline]
admin.site.register(FlatPage, FlatPageAdmin)
当我加载管理页面时,我收到:
AlreadyRegistered at /admin/
The model FlatPage is already registered
Exception Value:The model FlatPage is already registered
谢谢!
最佳答案 您必须首先取消注册它,因为应用程序本身附带了admin.py
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, FlatPageAdmin)