我想按当天的“今天”和“计数ID”进行过滤

时间:2019-12-02 06:10:29

标签: python django

我是Django Plz的新手,请帮帮我 但是在这种情况下,我希望结果为

{weekday: todayweek_number, count: total_ids_count_number }

当前,我正在按以下方式使用此Django查询

 Booking.objects.filter(created_at__day=today.day).annotate(count=Count('id')).values('weekday', 'count') 

这里要点是“星期几”,我希望今天星期几是我在过滤器中使用的键

型号:

consultation_type = models.CharField(max_length=50, default="OFFLINE", choices=CONSULTATION)
  conversation_type = models.CharField(max_length=50, default="HOMEVISIT", choices=CONVERSATION)
  patient = models.ForeignKey(Patient,verbose_name=_('Patient'), null=True, on_delete=models.SET_NULL)
  doctor = models.ForeignKey(Doctor, verbose_name=_('Doctor'), null=True, on_delete=models.SET_NULL)
  visit_address = models.TextField(verbose_name=_('Address'))
  appointment_at = models.DateTimeField(verbose_name=_('Appointment at'),editable=True, blank=True,null=True)
  estimated_cost = models.FloatField(verbose_name=_('Estimated Cost'),default=0,validators=[MinValueValidator(0), MaxValueValidator(99999)])
  booking_status = models.CharField(verbose_name=_('Booking Status'), max_length=50, default= "WAITING", choices=BOOKINGSTATUS)
  cancel_reason = models.CharField(verbose_name=_('Cancel Reason'),max_length=200)
  comment = models.TextField(verbose_name=_('Comment'))
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now=True)
  service = models.ForeignKey(to="doctors.Services", verbose_name=_('Service'), null=True, on_delete=models.SET_NULL)
  child = models.ManyToManyField(verbose_name=_('Child'),to="patients.Child", blank=True)
  payment_status = models.CharField(verbose_name=_('Payment Status'),max_length=50, default= "PENDING", choices=BOOKINGPAYMENTSTATUS)

错误:

Cannot resolve keyword 'weekday' into field.

3 个答案:

答案 0 :(得分:1)

django提供了名为ExtractWeekDay的数据库功能。您可以像这样使用它:

from django.db.models.functions import ExtractWeekDay

Booking.objects.filter(created_at__day=today.day).annotate(count=Count('id'),weekday=ExtractWeekDay('created_at')).values('weekday', 'count') 

更新

据我所知,从OP的评论来看,您无法像使用查询集的[0, 32, 0, 0, 0, 0, 0, 0]一样。但是通过循环,您可以这样实现:

bookings = list(Booking.objects.filter(created_at__day=today.day).annotate(count=Count('id'),weekday=ExtractWeekDay('created_at')).values_list('weekday','count'))

weekdays = [0, 0, 0, 0, 0, 0, 0]
for i in bookings:
    weekdays[i[0]] = i[1]

OR 只需使用以下代码:

weekdays = [0, 0, 0, 0, 0, 0, 0]
weekdays[today.weekday()] = Booking.objects.filter(created_at__day=today.day).count()
previous_day = today - datetime.timedelta(days=1)
weekdays[previous_day.weekday()] = Booking.objects.filter(created_at__day=previous_day.day).count()

答案 1 :(得分:0)

更新代码:

from django.db.models import Count
from django.db.models.functions import ExtractWeekDay
from datetime import date
today = date.today()

Mymodel.objects.filter(created_at__day=today.day).annotate(count=Count('id'),weekday=ExtractWeekDay('created_at')).values('weekday', 'count') 

结果:

{'count': 1, 'weekday': 2}

答案 2 :(得分:0)

如果您使用的是Django 2,则可以使用ExtractWeekDay对字段进行分组并计算数量。

喜欢:

from django.db.models.functions import ExtractWeekDay
Booking.objects.filter(created_at__day=today.day).annotate(weekday=ExtractWeekDay('created_at')).values('weekday').annotate(count=Count('id')).values('weekday', 'count')