Django 中的通用关系,带有聚合的查询集
Generic relation in django, queryset with aggregate
我在模型中使用 django 1.6 和通用关系。我对模型中的 aggregate
函数有疑问:
以下是我的部分观点:
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']
我在这个地方遇到错误:
minmax_sales_events_activities = sales_events_activities.aggregate(Min('start'), Max('end'))
错误:
no such table: events_saleandcycle
但是数据库(我使用sqlite)中有table字段: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)
有一个 closed issue against Django 1.6 that describes failures of annotations (aggregations). The relevant commit 修复仅存在于 Django 1.7a 向上。
恐怕你唯一的机会就是升级到 Django 1.7。
我在模型中使用 django 1.6 和通用关系。我对模型中的 aggregate
函数有疑问:
以下是我的部分观点:
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']
我在这个地方遇到错误:
minmax_sales_events_activities = sales_events_activities.aggregate(Min('start'), Max('end'))
错误:
no such table: events_saleandcycle
但是数据库(我使用sqlite)中有table字段: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)
有一个 closed issue against Django 1.6 that describes failures of annotations (aggregations). The relevant commit 修复仅存在于 Django 1.7a 向上。
恐怕你唯一的机会就是升级到 Django 1.7。