Database design for a website that sells tickets to events with Django
I am working on a website that sells tickets for events. And I have the following design:
And models (simplified for posting):
class Event(models.Model):
name = models.CharField(max_lenght=20)
class Date(models.Model):
event_start = models.DateTimeField()
event = models.ForeignKey(Event)
class Ticket_Class(models.Model):
name = models.CharField(max_lenght=20)
price = models.IntegerField()
event = models.ForeignKey(Event)
- How do I add a ticket model? Since the Ticket is for an event on a specific date, so I will need to associate the ticket with the relationship between the event and the date
- The Ticket_Class should also have a "max" field that stores the maximum tickets available for sale for that particular Ticket_Class on a particular date. For example: Event "Metallica" has Ticket_Class "Field" and Ticket_Class "VIP" as on three different dates. How can I access the number of remaining tickets for Ticket_Class for a specific date? Perhaps counting the number of tickets in the ticket table corresponding to that event on that date.
I hope I realized that English is not my first language. Feel free to ask any doubts.
source to share
I think your design should be something like this:
class Event(models.Model):
name = models.CharField(max_lenght=20)
class Date(models.Model):
event_start = models.DateTimeField()
event_end= models.DateTimeField()
class Ticket_Class(models.Model):
name = models.CharField(max_lenght=20)
price = models.IntegerField()
type= models.CharField(max_lenght=20)
class EventTicketSell(models.Model):
event= models.ForeignKey(Event)
date= models.ForeignKey(Date)
ticket= models.ForeignKey(Ticket_Class)
max_sellable_tickets= models.IntegerField()
The reason for this design is that you can add an event to the EventTicketSell class and then assign a date and ticket with the highest selling tickets.
to add ticket / date / event:
event1= Event(name= "Lion King")
event1.save()
event2= Event(name= "Metallica")
event2.save()
vip_ticket= Ticket_Class(name='VIP', price= 100, type='VIP')
vip_ticket.save()
evening_show= Date(event_start='Date Object', event_end= 'Date Object') # Date Object is like datetime.datetime.now()
evening_show.save()
concert_ticket_sell= EventTicketSell(event=event1, ticket=vip_ticket, date= evening_show, max_sellable_ticket=500)
concert_ticket_sell.save()
movie_ticket_sell= EventTicketSell(event=event2, ticket=vip_ticket, date= evening_show, max_sellable_tickets=500)
movie_ticket_sell.save()
This construct will retain objects that can be reused and modified flexibly. For example, if you want to change max_sellable_ticket, then:
movie_ticket_sell= EventTicketSell.objects.filter(event__name='Lion King', date__event_start= datetime.datetime.now(), ticket__type= 'VIP')[0]
movie_ticket_sell.max_sellable_tickets -= form.cleaned_data['ticket_sold'] #for example we get sold count from form
movie_ticket_sell.save()
source to share
It seems that the event should have a ticket count field to check what the maximum tickets are. Then it must have a foreign key relationship with ticket_class. To make sure you don't exceed the number of available tickets, you will probably need to use something like a clean method to make sure it doesn't exceed when you associate a ticket with an event.
https://docs.djangoproject.com/en/1.5/ref/models/instances/#django.db.models.Model.clean
I'm just looking at this from a high level, so maybe I am missing something. I also don't understand why you can't just put the date in the field?
source to share