Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving unique results in Django queryset based on column contents
    primarykey
    data
    text
    <p>I am not sure if the title makes any sense but here is the question.</p> <p>Context: I want to keep track of which students enter and leave a classroom, so that at any given time I can know who is inside the classroom. I also want to keep track, for example, how many times a student has entered the classroom. This is a hypothetical example that is quite close to what I want to achieve.</p> <p>I made a table Classroom and each entry has a Student (ForeignKey), Action (enter,leave), and Date.</p> <p>My question is how to get the students that are currently inside (ie. their enter actions' date is later than their leave actions' date, or don't have a leave date), and how to specify a date range to get the students that were inside the classroom at that time.</p> <hr> <p><strong>Edit</strong>: On better thought I should also add that there are more than one classrooms. </p> <p>my first attempt was something like this:</p> <pre><code>students_in = Classroom.objects.filter(classroom__exact=1, action__exact='1') students_out = Classroom.objects.filter(classroom__exact=1, action__exact='0').values_list('student', flat=True) students_now = students_in.exclude(student__in=students_out) </code></pre> <p>where if <code>action</code> == 1 is in, 0 is out.</p> <p>This however provides the wrong data as soon as a student leaves a classroom and re-enters. She is listed twice in the <code>students_now</code> queryset, as there are two 'enters' and one 'leave'. Also, I can't check upon specific date ranges to see which students have an entry date that is later than their leave date.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload