Django - 内联(没有ForeignKey)

时间:2011-12-15 20:28:40

标签: python django

我正在尝试实现InlineModels,但我收到了错误消息。你能帮帮我吗?谢谢。

myproject.api.models

from django.db import models
from django.contrib.auth.models import User
from myproject.song.models import Album, Song, Artist

class UserLibrary(models.Model):
    user = models.ForeignKey(User)

myproject.api.admin.py

from django.contrib import admin
from myproject.song.models import Song, Album, Artist, Music
from myproject.api.models import UserLibrary

class SongInline(admin.TabularInline):
    model = Song
class UserLibraryAdmin(admin.ModelAdmin):
    search_fields = ['user']
    inlines = [
        SongInline,
        ]
admin.site.register(UserLibrary, UserLibraryAdmin)

这是我的追溯:

 Exception at /admin/api/userlibrary/add/
<class 'myproject.song.models.Song'> has no ForeignKey to <class 'api.models.UserLibrary'>

1 个答案:

答案 0 :(得分:3)

你建立了错误的关系 - 你的Song模型应该与UserLibrary模型有外键关系,而不是相反。除此之外,一切似乎都是正确的。