【问题标题】:Filter Creation Date By Time Range按时间范围筛选创建日期
【发布时间】:2020-01-13 14:55:48
【问题描述】:

Im 试图过滤我的前两个 IF 语句,只显示今天当前日期从上午 8 点到上午 10 点的 created_at 记录,现在它只显示日期,我需要将 date= date 更改为 created_at = ' ' 然而,这将在一个范围内完成。这是我的views.py。如何将此添加到我的 IF 语句中?谢谢

views.py

def Student_Progress(request, studentpsid):

    if request.method == "GET":

        date = datetime.date.today()
        # Calculates Points on Student Progress Page
        academic = K8Points.objects.values_list('academic', flat=True).filter(
            student_name_id=studentpsid).filter(date=date)
        behavior = K8Points.objects.values_list('behavior', flat=True).filter(
            student_name_id=studentpsid).filter(date=date)
        my_class_id = request.session['my_class_id']
        academic_total = 0
        behav_total = 0
        for score in academic:
            academic_total += int(score)
        for score in behavior:
            behav_total += int(score)
        grand_total = academic_total + behav_total
        counseling = Student.objects.values_list(
            'counseling_goal', flat=True).get(studentpsid=studentpsid)
        studentid = Student.objects.get(studentpsid=studentpsid)
        k8obj = K8Points.objects.filter(
            student_name_id=studentpsid, date=date).order_by('time_frame')
        # Checks To See If a Student Gets Morning Recess
        if grand_total >= 20 and K8Points.objects.filter(time_frame=1).filter(date=date) and K8Points.objects.filter(time_frame=2).filter(date=date) and K8Points.objects.filter(time_frame=3).filter(date=date):
            morning_recess = "YES"
            context = ({'studentid': studentid, 'date': date, 'counseling': counseling, 'grand_total': grand_total, 'academic_total': academic_total,
                        'behav_total': behav_total, 'k8obj': k8obj, 'my_class_id': my_class_id, 'morning_recess': morning_recess})
            return render(request, 'points/student_progress.html', context)
        if grand_total <= 20 and K8Points.objects.filter(time_frame=1) and K8Points.objects.filter(time_frame=2) and K8Points.objects.filter(time_frame=3).filter(date=date):
            morning_recess = "NO"
            context = ({'studentid': studentid, 'date': date, 'counseling': counseling, 'grand_total': grand_total, 'academic_total': academic_total,
                        'behav_total': behav_total, 'k8obj': k8obj, 'my_class_id': my_class_id, 'morning_recess': morning_recess})
            return render(request, 'points/student_progress.html', context)
        else:
            context = ({'studentid': studentid, 'date': date, 'counseling': counseling, 'grand_total': grand_total, 'academic_total': academic_total,
                        'behav_total': behav_total, 'k8obj': k8obj, 'my_class_id': my_class_id, })
            return render(request, 'points/student_progress.html', context)


models.py

class K8Points(models.Model):
    date = models.DateField(default=datetime.date.today()) 
    class_name = models.ForeignKey(TeacherClass, on_delete = models.PROTECT, default = "",)
    student_name = models.ForeignKey(Student,on_delete = models.CASCADE, default ="" ,) 
    week_of = models.IntegerField(default=weeknumber)
    day = models.CharField(max_length= 10, default = dayofweek)
    TIME_FRAME_CHOICES = [
        (None, 'PLEASE SELECT TIME FRAME'),  # THIS IS OPTIONAL
        (1, '(1.) 8:45AM - 9:00AM'),
        (2, '(2.) 9:00AM - 9:30AM'),
        (3, '(3.) 9:30AM - 10:00AM'),
        (4, '(4.) REC. I 10:00AM -10:10AM'),
        (5, '(5.) 10:10AM-10:40AM'),
        (6, '(6.) 10:40AM-11:10AM'),
        (7, '(7.) 11:10AM-11:40AM'),
        (8, '(8.) REC II LUNCH 11:40AM-12:20PM'),
        (9, '(9.) 12:20PM-12:50PM'),
        (10,'(10.) 12:50PM-1:20PM'),
        (11,'(11.) 1:20PM-1:50PM'),
        (12,'(12.) 1:50PM-2:20PM'),
        (13,'(13.) REC. III 2:20PM-2:30PM'),
    ]
    time_frame = models.PositiveSmallIntegerField(choices=TIME_FRAME_CHOICES,)
    behavior = models.IntegerField(default="", validators=[
            MaxValueValidator(5),
            MinValueValidator(1)
        ])
    academic = models.IntegerField(default="", validators=[
            MaxValueValidator(5),
            MinValueValidator(0)
        ] )
    total = models.IntegerField(default=0 )
    morning_recess= models.CharField(max_length= 3, blank = True ,default = "" )
    created_at = models.DateTimeField(auto_now_add=True)


【问题讨论】:

    标签: django django-models django-views


    【解决方案1】:

    今天的过滤器

    today_date = datetime.datetime.now().date()
    
    .filter(your_datetime_field__date = today_date)
    

    小时过滤器

    .filter(your_datetime_field__hour__lte = 10).filter(your_datetime_field__hour__gte = 8)
    

    请记住,10:59:59 也会出现在此查询的结果中。 如果您只需要 8:00:00 - 9:59:59,请使用 lt 而不是 lte

    【讨论】:

    • if grand_total &gt;= 20 and K8Points.objects.filter(time_frame=1).filter(date=date).filter(created_at__hour__lte=13).filter(created_at__hour__gte=8) and K8Points.objects.filter(time_frame=2).filter(date=date).filter(created_at__hour__lte=13).filter(created_at__hour__gte=8) and K8Points.objects.filter(time_frame=3).filter(date=date).filter(created_at__hour__lte=13).filter(created_at__hour__gte=8):
    • 我猜是因为您过滤filter(date=date) 而不是filter(date__date=date),就像我写的那样。如果使用此修复程序仍然无法正常工作,请将您的模型类添加到问题中。
    • ``` today_date = datetime.datetime.now().date() if grand_total >= 20 and K8Points.objects.filter(time_frame=1).filter(created_at__date = today_date).filter( created_at__hour__lt=13).filter(created_at__hour__gte=8) 和 K8Points.objects.filter(time_frame=2).filter(created_at__date = today_date).filter(created_at__hour__lte=13).filter(created_at__hour__gte=8) 和 K8Points.objects.filter( time_frame=3).filter(created_at__date = today_date).filter(created_at__hour__lte=13).filter(created_at__hour__gte=8):```
    • 还是什么都没有,我也是 django 新手,有没有更简洁的方法可以写 if 语句?
    • Star 问题不是您给我的代码,我正在查看数据库并在 created_at 字段中发布错误的时间,但是在开发服务器中,当它向您显示获取和发布时间戳时,它是正确的.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多