Reputation: 15
I have to create a registration app using Django. There will be two models: Event, consisting of event name and fees; and Participant, consisting of participant details and the event in which they have participated. One participant may have participated in multiple events. While saving the objects I do not want multiple entries for different events of the same participant. I suppose this implies the use of a one to many relationship field, which I am unable to find in Django. The schema I tried was
class Event(models.Model):
name = models.CharField(max_limit=50)
fees = models.IntegerField(default=100)
class Participant (models.Model):
name = models.CharField(max_limit=50)
event = models.ForeignKey(Event)
receiptno = models.IntegerField(primary_key=True)
I tried using Foreign key field but it is able to store only one event the participant has participated in. I want to be able to store multiple events in one participant object (as only one receipt was issued manually per participant irrespective of the events participated in) How do I do the same?
Upvotes: 0
Views: 133
Reputation: 599610
You want a many-to-many relationship, not a one-to-many one. Specifically, you want one with a through model, which would record the receipt number.
class Participant(models.Model):
name = models.CharField(max_limit=50)
event = models.ManyToManyField('Event', through='Receipt')
class Receipt(models.Model):
receiptno = models.IntegerField()
event = models.ForeignKey('Event')
participant = models.ForeignKey('Participant')
Upvotes: 2