Darren-Jaen
Darren-Jaen

Reputation: 135

Getting the amount of guests in bookings made django

If each a user goes into an event they can make an booking, but each event only has a certain amount of space open.

I would like to show in the event that there are 5 out of 10 seats left. I just can't seem to find the sum of guests that have already booked with a status of being active or pending.

here is my events model

class Events(models.Model):

ACTIVE = (('d', "Deactivated"), ('e', "Expired"), ('a', "Active"), ('b', "Drafts"),)
ALCOHOL = (('0','No bring own alcohol'),('1','There will be complimentary wine pairing'))

user = models.ForeignKey(User, on_delete=models.CASCADE)
active = models.CharField(max_length=1, default='b', choices=ACTIVE)
title = models.CharField(max_length=50, blank=True, default='')
description = models.TextField(max_length=500, blank=True, default='')
date = models.DateField()
time = models.TimeField()
price = models.CharField(max_length=240, blank=True, default='')
seats = models.IntegerField()
alcohol_choice = models.CharField(max_length=1, default='n' ,choices=ALCOHOL)
starter = models.TextField(max_length=350, blank=True, default='')
main_menu = models.TextField(max_length=350, blank=True, default='')
dessert = models.TextField(max_length=350, blank=True, default='')
notes = models.TextField(max_length=350, blank=True, default='')
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)

@property
def bookings_total(self):
    return self.user.bookings_set.filter(bookingstatus='y').count()

here is my bookings model

class Bookings(models.Model):

OPTIONS_STATUS = (('y', "Yes"), ('n', "No"), ('p', "Pending"),)

user = models.ForeignKey(User, on_delete=models.CASCADE)
event = models.ForeignKey(Events, on_delete=models.CASCADE)
eventdate = models.DateField()
event_amount = models.CharField(max_length=50, blank=True, default='')
guests = models.IntegerField()
bookingstatus = models.CharField(max_length=50, default='p', blank=True, choices=OPTIONS_STATUS)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)

Here is my views code

 today = datetime.now().strftime('%Y-%m-%d')

 events_list_data = Events.objects.filter(active='a').filter(Q(date__gte=today)|Q(date=today)).order_by('date')

here is my template code

 {% for event_list in events_list_data %}
    -- CODE GOES HERE --
    {{ event_list.bookings_total }} Seats Left
 {% endfor %} 

Upvotes: 1

Views: 440

Answers (2)

user8060120
user8060120

Reputation:

if i correctly understand, you can try

@property
def bookings_total(self):
    return self.bookings_set.filter(bookingstatus='y').count()

@property
def bookings_left(self):
    return self.bookings_total - self.seats

Upvotes: 1

Astik Anand
Astik Anand

Reputation: 13057

Try:

 events = Events.objects.filter(active='a').\
 filter(Q(date__gte=today)|Q(date=today)).order_by('date')

 events = events.filter(Q(booking__bookingstatus='n')|Q(booking__bookingstatus='p'))
 events = events.annotate(num_of_seats=Count('booking'))

and now you can use, .num_of_seats to show no of seats in an event.

In html file:

{% for event in events %}
      {{event.title}}
      {{even.num_of_seats}}
{% endfor %}

Upvotes: 1

Related Questions