1

我必须在不同的模型中上课。第一个,叫有声读物

from django.db import models
from labels.models import Label


class Audiobook(models.Model):
    """the audiobook object"""
    title = models.TextField()
    content = models.TextField()
    cover = models.FilePathField()
    playtime = models.TextField()
    moneyLink = models.TextField()
    status = models.BooleanField(default=False)
    label = models.ForeignKey(Label)

第二个,叫做标签

from django.db import models


class Label(models.Model):
    """the label object"""
    name = models.TextField()
    description = models.TextField()
    status = models.BooleanField(default=False)

在有声读物中,我在 Label 对象上有一个 ForeignKey。

现在是管理类。

有声读物管理员:

from audiobooks.models import Audiobook
from labels.models import Label
from django.contrib import admin


class LabelInline(admin.TabularInline):
    model = Label


class AudiobookAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields': ['title']}),
        (None, {'fields': ['content']}),
        (None, {'fields': ['cover']}),
        (None, {'fields': ['playtime']}),
        (None, {'fields': ['moneyLink']}),
        (None, {'fields': ['status']}),
        (None, {'fields': ['label']})
    ]
    inlines = [LabelInline]

admin.site.register(Audiobook, AudiobookAdmin)

标签管理类:

from labels.models import Label
from django.contrib import admin


class LabelAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields': ['name']}),
        (None, {'fields': ['description']}),
        (None, {'fields': ['status']})
    ]

admin.site.register(Label, LabelAdmin)

Label 部分一切正常。

我必须做什么才能在有声读物管理页面中选择带有下拉控件的标签?如果我不想输入新的有声读物,则会出现以下错误:

Exception Value: <class 'labels.models.Label'> has no ForeignKey to <class 'audiobooks.models.Audiobook'>

感谢您的每一个帮助!

4

1 回答 1

0

Dropdown 控件是 ForeignKey 的默认控件。

默认管理员配置应该可以工作:

from labels.models import Label
from audiobooks.models import Audiobook
from django.contrib import admin

admin.site.register(Label)
admin.site.register(Audiobook)
于 2013-07-14T09:31:00.573 回答