使用 Django 计数和 Q/F 查询
Count and Q/F query with django
我想将 Q/F 个对象与计数一起使用,但出现以下错误:
int() argument must be a string or a number, not 'Count'
我想获得所有交易:
- start_date 是过去或今天的日期
- end_date 是未来或今天的日期或
参与(DealsParticipation 模型)低于最大值
交易参与者数量(字段 nb_participants)。
我的模特:
class Deals(models.Model):
u"""List of deals available to the participant
"""
brand = models.ForeignKey(Brand_Advertiser, blank=False, null=False, related_name='brands_of_deal')
start_date = models.DateField(_("Date de debut"), blank=False, null=False, default=datetime.date.today)
end_date = models.DateField(_("Date de fin"), blank=True, null=True)
nb_participants=models.PositiveIntegerField(_("Nombre de participants maximum"), default=100, blank=True, null=True)
class DealsParticipation(TimeStampedModel):
u"""Participation to deals by participants
"""
deal = models.ForeignKey('Deals', blank=False, null=False, related_name='deal_participation')
participant = models.ForeignKey(Participant, blank=False, null=False)
我尝试过的:
class CurrentDealsView(ListView):
model = models.Deals
def get_queryset(self):
#get all the deals having started in the past or today and closing in the future or today
now = datetime.now()
start_date_condition=Q(start_date__lte=now)
end_date_condition = Q(end_date__gte=now) | Q(end_date__isnull=True)
#get all the deals with a number of participations lower than the maximal number of participants
nb_participants_condition=Q(nb_participants__gt=Count('deal_participation'))
return models.Deals.objects.filter(start_date_condition & (end_date_condition | nb_participants_condition))
已解决!
正如 emulbreh 所解释的,我不得不在注释中使用计数:
def get_queryset(self):
#get all the deals having started in the past or today and closing in the future or today
now = datetime.now()
start_date_condition=Q(start_date__lte=now)
end_date_condition = Q(end_date__gte=now) | Q(end_date__isnull=True)
date_conditions=models.Deals.objects.filter(start_date_condition & end_date_condition)
#get all the deals with a number of participations lower than the maximal number of participants
return date_conditions.annotate(participation_count=Count('deal_participation')).filter(nb_participants__gt=F('participation_count'))
聚合函数(如Count
)需要与annotate()
或aggregate()
一起使用。您可以从 filter()
或 Q()
:
引用带注释的字段
Deals.objects.annotate(
participation_count=Count('deal_participation')
).filter(
nb_participants__gt=F('participation_count')
)
我想将 Q/F 个对象与计数一起使用,但出现以下错误:
int() argument must be a string or a number, not 'Count'
我想获得所有交易:
- start_date 是过去或今天的日期
- end_date 是未来或今天的日期或 参与(DealsParticipation 模型)低于最大值 交易参与者数量(字段 nb_participants)。
我的模特:
class Deals(models.Model):
u"""List of deals available to the participant
"""
brand = models.ForeignKey(Brand_Advertiser, blank=False, null=False, related_name='brands_of_deal')
start_date = models.DateField(_("Date de debut"), blank=False, null=False, default=datetime.date.today)
end_date = models.DateField(_("Date de fin"), blank=True, null=True)
nb_participants=models.PositiveIntegerField(_("Nombre de participants maximum"), default=100, blank=True, null=True)
class DealsParticipation(TimeStampedModel):
u"""Participation to deals by participants
"""
deal = models.ForeignKey('Deals', blank=False, null=False, related_name='deal_participation')
participant = models.ForeignKey(Participant, blank=False, null=False)
我尝试过的:
class CurrentDealsView(ListView):
model = models.Deals
def get_queryset(self):
#get all the deals having started in the past or today and closing in the future or today
now = datetime.now()
start_date_condition=Q(start_date__lte=now)
end_date_condition = Q(end_date__gte=now) | Q(end_date__isnull=True)
#get all the deals with a number of participations lower than the maximal number of participants
nb_participants_condition=Q(nb_participants__gt=Count('deal_participation'))
return models.Deals.objects.filter(start_date_condition & (end_date_condition | nb_participants_condition))
已解决!
正如 emulbreh 所解释的,我不得不在注释中使用计数:
def get_queryset(self):
#get all the deals having started in the past or today and closing in the future or today
now = datetime.now()
start_date_condition=Q(start_date__lte=now)
end_date_condition = Q(end_date__gte=now) | Q(end_date__isnull=True)
date_conditions=models.Deals.objects.filter(start_date_condition & end_date_condition)
#get all the deals with a number of participations lower than the maximal number of participants
return date_conditions.annotate(participation_count=Count('deal_participation')).filter(nb_participants__gt=F('participation_count'))
聚合函数(如Count
)需要与annotate()
或aggregate()
一起使用。您可以从 filter()
或 Q()
:
Deals.objects.annotate(
participation_count=Count('deal_participation')
).filter(
nb_participants__gt=F('participation_count')
)