0

I have a DayArchiveView where I pass in a queryset of booking slots. I would like to be able to pass in some context too, like more data. Specifically, I would like to pass the earliest time, latest time and a few other small calculations.

My view is currently this.

class BookingArchiveView(DayArchiveView):
     queryset = BookingSlot.objects.filter(location__club__id=1)
     date_field = "start_time"
     ordering = ['location']
     #I would like to pass in another object like so
     context["earliest_time"]=BookingSlot.objects.filter(location__club__id=1).annotate(Min('start_time'))

So I would like to be able to pass in some aggregate functions and calculations that I could render in html. Right now, the HTML only has access to my queryset.

2 Answers2

0

Have you tried to override get_context_data() method? In this method you should add any extra data you want to pass to html template. Another way is to update your context in get() method, but in this case the first solution will be better.

More details you can find here When to use get, get_queryset, get_context_data in Django?

0

Add a get_context_data() to your CBV

Example below

def get_context_data(self, *, object_list=None, **kwargs):
    context = super().get_context_data(**kwargs)
    context['calculation'] = some_random_number
    return context

Then you can access variables you declared in your get_context_data() in your html's like {{ calculation }}