从具有2个一对多关系的表中获取结果

时间:2010-08-05 16:01:26

标签: django

在我的应用程序中,我有一个看起来像这样的模型:

class Talk(models.Model):
    title = models.CharField(max_length=200, primary_key=True)
    speaker = models.CharField(max_length=200)
    date_of_talk = models.DateField('date_of_talk')
    def __unicode__(self):
        return self.title

class Member(models.Model):
    name = models.CharField(max_length=200)
    telephone_number = models.CharField(max_length=200)
    email_address = models.CharField(max_length=200)
    membership_type = models.CharField(max_length=1, choices=MEMBERSHIP_TYPES)
    membership_number = models.CharField(max_length=200, primary_key=True)
    def __unicode__(self):
        return self.name    
class Event_Attendance(models.Model):
    talk = models.ForeignKey('Talk')
    membersAttended = models.ForeignKey('Member')  
    def __unicode__(self):
    return '%s attended %s' %(unicode(self.membersAttended), unicode(self.talk))

当我输入谈话时,我希望能够从Event_Attendance模型中提取所有结果。这样我就可以得到参加它的人的名字。我尝试过的每件事都失败了,例如:

 a = Event_Attendance(talk="Meteors")

有没有人有任何想法?
在此先感谢,
迪恩

1 个答案:

答案 0 :(得分:1)

我认为您的EventAttendance模型中的members_attended字段会更好,因为ManyToManyField

class EventAttendance(models.Model):
    talk = models.ForeignKey(Talk)
    members_attended = models.ManyToManyField(Member)

这样可以轻松获取所需信息。