Django - 内联(没有外键) [英] Django - Inline (has no ForeignKey to)

查看:251
本文介绍了Django - 内联(没有外键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to implement InlineModels but I'm getting error message. Will you please help me? Thank you .

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



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'>


推荐答案

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

You've built the wrong relationship--your Song model should have a foreign key relationship to the UserLibrary model, not the other way around. Other than that, everything seems to be correct.

这篇关于Django - 内联(没有外键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆