1

I have two filters in my code here, students filters everyone in that particular class and students_wr is another filter that quieres my table K8Recess which logs all the students in the school who got recess for that day. What I want to do is combine these filters.

So show everyone in my class who got recess for the day . How do i do that ? Here is my Code

def K8_Recess_Report(request, classid):
    if request.method == "GET":
       date = datetime.date.today()
       class_name = TeacherClass.objects.get(id=classid)
       getstudents = Student.objects.filter(class_name=classid)
       students = getstudents.all().order_by('student_name')
       students_wr = K8Recess.objects.filter(created_at__date = date )
       my_class_id = request.session['my_class_id']
       context = ({'students': students, 'class_name': class_name, 'my_class_id': my_class_id, 'date': date,})
    return render(request, 'points/k8_recess_report.html', context)  

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.) LUNCH 11:40AM-12:00PM'),
        (9, '(9.) REC. II 12:00PM-12:20PM'),
        (10, '(10.) 12:20PM-12:50PM'),
        (11,'(11.) 12:50PM-1:20PM'),
        (12,'(12.) 1:20PM-1:50PM'),
        (13,'(13.) 1:50PM-2:20PM'),
        (14,'(14.) 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)
        ] )

    created_at = models.DateTimeField(default=datetime.datetime.now())


    class Meta:
        verbose_name = "K8-Points"


    def __str__(self):
        return self.student_name  

class K8Recess(models.Model):
    student_ps = models.ForeignKey(Student,on_delete = models.CASCADE, default ="" ,) 
    created_at = models.DateTimeField(default=datetime.datetime.now())
    morning_recess = models.BooleanField(blank= True, null = True)
    lunch_recess = models.BooleanField(blank= True, null = True)
    afternoon_recess = models.BooleanField(blank= True, null = True)

enter image description here

8
  • I guess you have a student field on K8Recess model. If this is so, you can do this: students_recess_today = K8Recess.objects.filter(student__class_name=classid, created_at__date=date). Let me know if it works. Commented Feb 5, 2020 at 14:49
  • I will post my models hold on so you see the setup. Commented Feb 5, 2020 at 14:53
  • Well, so you have student_ps (instead of student as I suggested) field that is a ForeignKey of Student class. So you can do K8Recess.objects.filter(student_ps__class_name=classid, created_at__date=date). Check if it works, please. Commented Feb 5, 2020 at 14:58
  • Awesome that worked however is there anyway to show that on one line as apposed to individual lines ? Commented Feb 5, 2020 at 15:02
  • Happy to hear that it worked! I don't understand your last question though, sorry... Commented Feb 5, 2020 at 15:05

1 Answer 1

1

You can get this by:

K8Recess.objects.filter(student_ps__class_name=classid, created_at__date=date)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.