pythondjangoaggregatedjango-1.6django-generic-relations

Generic relation in django, queryset with aggregate


I use django 1.6 and generic relation in models. And I have problem with aggregate function in model:

Here is part of my view:

class EventListView(PageContextMixin, ListView):
    model = Activity
    template_name = 'events/eventlist.html'

    def get_queryset(self):
        seasons = Season.objects.all()
        if not self.request.user.is_superuser:
            seasons = seasons.filter(is_active=True)
            active_seasons_ids = list(IsActiveFlag.objects.exclude(is_active=False).values_list('id', flat=True))
            seasons = list(Season.objects.filter(isactiveflags__id__in=active_seasons_ids))
        minmax = seasons.aggregate(Min('start'), Max('end'))
        min_date = minmax['start__min']
        max_date = minmax['end__max']

        active_sale_and_cycles_ids = list(SaleAndCycle.objects.exclude(is_sale_active=False).values_list('id', flat=True))
        filters_sale = Q(sale_and_cycles__id__in=active_sale_and_cycles_ids)
        active_is_cyclic_event_ids = list(SaleAndCycle.objects.exclude(is_cyclic_event_active=False).values_list('id', flat=True))
        filters_cycle = Q(sale_and_cycles__id__in=active_is_cyclic_event_ids)
        sales_events_activities = Activity.objects.filter(Q(filters_sale, online=True) | Q(filters_cycle, online=True))   
        sales_events_ids = sales_events_activities.values_list('id', flat=True)
        minmax_sales_events_activities = sales_events_activities.aggregate(Min('start'), Max('end'))
        max_date_sales_events_activities = minmax_sales_events_activities['end__max']

I get an error in this place:

minmax_sales_events_activities = sales_events_activities.aggregate(Min('start'), Max('end'))

Error:

no such table: events_saleandcycle

But there is table in database (I use sqlite) with fields: id, content_type_id, object_id, position, is_sale_active, is_cyclic_event_active, cycle_link

class SaleAndCycle(SortableVAExtra):
    is_sale_active = models.BooleanField(default=False)
    is_cyclic_event_active = models.BooleanField(default=False)
    cycle_link = models.CharField(max_length=255, null=True, blank=True)

    def __unicode__(self):
        return str(self.is_sale_active)

Solution

  • There is a closed issue against Django 1.6 that describes failures of annotations (aggregations). The relevant commit with the fix is only present in Django 1.7a upwards.

    I fear your only chance is to upgrade to Django 1.7.