Search code examples
pythondjangoforeign-keysmodels

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')
    comments = CharField(max_length=1024)
    def __unicode__(self):
        return name

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

Solution

  • 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.