我有模型模型 Word
,它具有模型 Group
的外键我希望django管理面板中的结构嵌套,如下所示:
-Group
-Word1 (clickable where I can show all the info about this word model)
-Word2 (clickable)
-Word3 (clickable)
我试着在admin.py中做这样的事情但是得到了一个错误:没有类元
from django.contrib import admin
from main_app.models import Profile, Project, User, Word, Phrase, Group
def unbound_callable(word):
return word.name
class WordInline(admin.TabularInline):
model = Word
fields = ('name', 'model_callable', 'model_admin_callable', unbound_callable)
readonly_fields = ('model_callable', 'model_admin_callable', unbound_callable)
def model_admin_callable(self, word):
return word.name
class GroupAdmin(admin.ModelAdmin):
model = Group
inlines = (Word,)
class Meta:
model = Group
admin.site.register([GroupAdmin, Word, Phrase])
1 回答
register()
方法作为第一个argrument模型列表,但是你在那里传递了admin类GroupAdmin
. 您可以使用register()
decorator与GroupAdmin
代替 . 并分别注册Word
和Phrase
型号:请注意,您无需在
ModelAdmin
class中指定model = Group
.