Thank you Simon.
I will take a look at annotations.
How would I represent seats remaining in the template for loop? Here is
what I have so far.....
#####################
{% for i in events %}
<div class='first-column'>
<h3 style='display:inline;'>{{ i.event_date|date:"l M j, Y" }} {{
i.event_time|date:"g:i A" }}</h3><br>
<p style='display:inline;'>{{i.event_type|title}} -
{{i.title|title}}</p><br>
<a style='display:inline;' href="{% url 'make_reservation' pk=i.pk
%}">Purchase Reservation ${{i.price}}</a> <p style='display: inline;'>Seats
Remaining ### VARIABLE HERE ###</p>
<p> {{ i.description|safe}} </p>
</div>
<div class='second-column' style="background-image:
url('../static/restaurant/images/event_images/{{i.id}}.jpg');
background-size: cover;">
</div>
<div class='first-column' style="height:10px; border: 0px solid black;">
</div>
<div class='second-column' style="height:10px; border: 0px solid black;">
</div>
{% endfor %}
#####################
On Thursday, September 6, 2018 at 8:04:36 AM UTC-5, Simon Charette wrote:
>
> Hello there,
>
> You should be able to achieve what you're after by using annotations[0].
>
> In your case you'll want to declare your Event and Reservation
> relationship explicitly by using
> a ForeignKey
>
> class Event(models.Model):
> ...
> seating = models.PositiveIntegerField(default=0)
>
> class Reservation(models.Model):
> ...
> event = models.ForeignKey(Event, related_name='reservations')
> seats = models.PositiveIntegerField(default=0)
>
> And perform the following query
>
> Event.objects.annotate(
> seats_remaining=F('seating') - Sum('reservations__seats').
> )
>
> Cheers,
> Simon
>
> [0]
> https://docs.djangoproject.com/en/2.1/topics/db/aggregation/#generating-aggregates-for-each-item-in-a-queryset
>
> Le jeudi 6 septembre 2018 07:42:36 UTC-4, [email protected] a écrit :
>>
>>
>> QUESTION
>>
>> I have an application that will make on-line reservations for a series of
>> weekly events. I would like to display the list of upcoming events in an
>> html template with the number of remaining seats available in a single html
>> page. I was able to create views to display the list of upcoming events and
>> another view to display the number of remaining seats available but I am
>> unable to figure out how to nest the two into a single view. Example
>> below...
>>
>> HTML OUTPUT
>>
>> Event Title Week 1 - x amount of seats remaining for this event
>>
>> Event Title Week 2 - x amount of seats remainign for this event
>>
>> Event Title Week 3 - x amount of seats remaining for this event
>>
>> and so on ....
>>
>> MODELS
>>
>> class Events(models.Model):
>> event_date = models.DateField(auto_now_add=False)
>> event_time = models.TimeField(auto_now_add=False)
>> event_type = models.CharField(max_length=20, choices=EVENT_TYPE)
>> seating = models.IntegerField(default=0)
>> title = models.CharField(max_length=200)
>> description = models.TextField()
>> menu = models.TextField()
>> price = models.DecimalField(max_digits=6, decimal_places=2)
>> publish = models.CharField(max_length=1, choices=PUBLISH_CHOICE)
>>
>> def __int__(self):
>> return self.title
>>
>> class Reservations(models.Model):
>> user_id = models.IntegerField(default=0)
>> event_id = models.IntegerField(default=0)
>> reservations = models.IntegerField(default=0)
>>
>> def __int__(self):
>> return self.event
>>
>>
>> VIEWS
>>
>> def events_view(request):
>>
>> events=Events.objects.filter(publish='Y').filter(event_date__gte=datetime.now()).order_by('event_date')
>>
>> reservation_count = Reservations.objects.aggregate(Sum('reservations'))
>>
>> return render(request, 'restaurant/events.html',{"events":events,
>> "reservation_count":reservation_count, })
>>
>> def make_reservation_view(request, pk):
>> event = Events.objects.get(id=pk)
>> seating_available = Events.objects.get(id=pk)
>> seating_available = seating_available.seating
>> reservation_count =
>> Reservations.objects.filter(event_id=pk).aggregate(res_sum=Sum('reservations'))
>> res = reservation_count['res_sum']
>> seats_remaining = seating_available - res
>>
>> return render(request, 'restaurant/make_reservation.html',
>> {"event":event, \
>>
>> "seats_remaining":seats_remaining,})
>>
>>
>>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/ffc7caf1-1d6b-4d3e-8cd9-8c458367d499%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.