2013-07-13 125 views
1

我必須在不同模型中的類。 第一個,稱爲有聲書django管理頁面下拉菜單

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) 

在有聲讀物我有一個標籤對象上一個ForeignKey。

現在管理員類。

audiobook_admin:

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) 

與標籤部分的所有罰款。

我必須做些什麼才能在Audiobook管理頁面中使用下拉控件選擇標籤? 如果我不會進入一個新的有聲讀物我以下錯誤:

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

感謝每一個幫助!

回答

0

下拉控件是ForeignKey的默認值。

默認管理員配置應該工作:

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

admin.site.register(Label) 
admin.site.register(Audiobook)