我正在尝试实现InlineModels
,但我收到了错误消息。你能帮帮我吗?谢谢。
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)
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'>
答案 0 :(得分:3)
你建立了错误的关系 - 你的Song
模型应该与UserLibrary
模型有外键关系,而不是相反。除此之外,一切似乎都是正确的。