Commit dd04fd98 authored by fred's avatar fred
Browse files

replace raw SQL query by a call to QuerySet.extra()

parent f1e04e0f
......@@ -49,17 +49,21 @@ class EmissionDetailView(DetailView):
context = super(EmissionDetailView, self).get_context_data(**kwargs)
context['add_schedule_form'] = ScheduleForm(initial={'emission': self.object})
context['schedules'] = Schedule.objects.filter(emission=self.object).order_by('datetime')
context['episodes'] = Episode.objects.raw('''
SELECT emissions_episode.*,
datetime AS first_diffusion
FROM emissions_episode, emissions_diffusion
WHERE emissions_episode.id = emissions_diffusion.episode_id
AND emissions_episode.emission_id = %s
AND datetime = (SELECT MIN(datetime)
FROM emissions_diffusion
WHERE episode_id = emissions_episode.id)
ORDER BY first_diffusion DESC''',
[self.object.id])
# get all episodes, with an additional attribute to get the date of
# their first diffusion
context['episodes'] = list(
Episode.objects.select_related().filter(emission=self.object
).extra(select={
'first_diffusion': 'emissions_diffusion.datetime',
},
select_params=(False, True),
where=['''datetime = (SELECT MIN(datetime)
FROM emissions_diffusion
WHERE episode_id = emissions_episode.id)'''],
tables=['emissions_diffusion'],
).order_by('-first_diffusion'))
return context
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment