开发者

Is there a field in django that can have multiple foreign key fields?

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')
  开发者_JS百科  comments = CharField(max_length=1024)
    def __unicode__(self):
        return name

class Person(Model):
    user = ForeignKey(User)
    friends =...  # multiple foriegn keys of itself


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜