django中是否有一个字段可以有多个外键字段?
django中是否有一个字段可以有多个外键字段?我有以下代码:
from django.db import models
from django.auth.models import *
class Wish(Model):
name = CharField(max_length=128)
cost = IntegerField()
person = ForeignKey(Person)
date = DateField('Date Wished')
comments = CharField(max_length=1024)
def __unicode__(self):
return name
class Person(Model):
user = ForeignKey(User)
friends =... # multiple foriegn keys of itself
Is there a field in django that can have multiple foreign key fields? I have the following code:
from django.db import models
from django.auth.models import *
class Wish(Model):
name = CharField(max_length=128)
cost = IntegerField()
person = ForeignKey(Person)
date = DateField('Date Wished')
comments = CharField(max_length=1024)
def __unicode__(self):
return name
class Person(Model):
user = ForeignKey(User)
friends =... # multiple foriegn keys of itself
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 ManyToMany 字段。
请注意,同一模型的 ManyToMany 被假定为 对称 - 如果 A 是 B 的朋友,那么 B 也将是 A 的朋友。您可以指定对称 = False 来避免这种情况。
Try using the ManyToMany field.
Note that ManyToMany to the same model, is assumed to be symmetrical - if Person A is a friend of Person B, then Person B will also be a friend of Person A. You can specify symmetrical=False to avoid that.
我认为你想要一个 ManyToMany 字段。在这个例子中,你会说一个人可以与许多其他人成为朋友,反之亦然。
Django 的 ManyToManyField:
https://docs。 djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField
一般ManyToMany:
http://en.wikipedia.org/wiki/Many-to-many_(data_model) )
I think you want a ManyToMany field. In this example you would be saying that a person can be friends with many other person's/people, and visa-versa.
Django's ManyToManyField:
https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField
General ManyToMany:
http://en.wikipedia.org/wiki/Many-to-many_(data_model)