简体   繁体   English

Django模型“有多个ForeignKey”

[英]Django Model “ has more than one ForeignKey to ”

there are two Models, User And Friends . 有两种型号, UserFriends I want to make two users make friends,but it did work. 我想让两个用户结交朋友,但确实有用。

the console just said, 控制台刚才说,

django.core.management.base.CommandError: System check identified some issues: django.core.management.base.CommandError:系统检查发现了一些问题:

ERRORS: : (admin.E202) 'api4android.Friends' has more than one ForeignKey to 'api4android.User'. 错误::(admin.E202)'api4android.Friends'对'api4android.User'有多个ForeignKey。

here is the code 这是代码

class Friends(models.Model):
    user = models.ForeignKey(User, null=True, related_name='user')
    friend = models.ForeignKey(User, null=True, related_name='friend')
    note_name = models.CharField(max_length=20)

def __str__(self):  
    return self.note_name

hope my poor english makes you understand what i mean. 希望我可怜的英语让你理解我的意思。

In your admin.py you have to specify fk_name to each relation. 在您的admin.py中,您必须为每个关系指定fk_name Example: 例:

class FriendshipInline(admin.TabularInline):
    model = Friendship
    fk_name = "to_person"

More info: Django Docs 更多信息: Django Docs

Change it to this 把它改成这个

class Friends(models.Model):
    user = models.ForeignKey(User, null=True)
    friend = models.ForeignKey(User, null=True, related_name='friend')
    note_name = models.CharField(max_length=20)

def __str__(self):  
    return self.note_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM