简体   繁体   中英

Django admin nested models

I have model model Word which has foreign key to model Group And I want the structure in django admin panel to be nested, like this:

-Group
 -Word1 (clickable where I can show all the info about this word model)
 -Word2 (clickable)
 -Word3 (clickable)

I tried to do something like this in admin.py but got an error: no class meta

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])

register() method takes as first argrument list of models, but you passed admin class GroupAdmin there. You can use register() decorator with GroupAdmin instead. And register Word and Phrase models separately:

@admin.register(Group)
class GroupAdmin(admin.ModelAdmin):
    inlines = (Word,)

admin.site.register([Word, Phrase])

Note you don't need to specify model = Group inside ModelAdmin class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM