views:

43

answers:

1

Hi,
In my django app i have the admin site and I am trying to customise it so that i can keep adding people to the event attendance. My models look like this:

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') 

I want to be able to configure the admin site so that i can keep adding members to the talk in the event_attendance object. I have one talk with many members. How would i do this? I also want the maximum number of membersAttended in the event_attendance object to be equal to the number of members.
Thanks in Advance,
Dean

+1  A: 

You need a ManyToManyField on Member using the "through" argument:

class Member(models.Model):
    [...]
    attending = models.ManyToManyField(Talk, through=Event_Attendance)

To take care of not allowing more than the maximum number of Members attend an event, you can just enforce a uniqueness constraint on Event_Attendance like so:

class Event_Attendance(models.Model):
    [...]
    class Meta:
        unique_together = ('talk', 'membersAttended')

With that you can be sure that each member can only attend a talk once, and you can happily query across any of your models to gain access to the data you're looking for.

Gabriel Hurley