Home > database >  Django Queryset to include and filter related data
Django Queryset to include and filter related data

Time:07-20

I'm trying to render a template with a list of participants (adult and minor) and their latest checkin and checkout times.

There are two types of participants and they are represented in two tables/models: AdultParticipant and MinorParticipant. MinorParticipants have a foreign key relationship to the ParentParticipant.

Checkin information (checkin and checkout) is stored in a single table irregardless of whether the checkin data refers to AdultParticipant or the MinorParticipant. One record in this table captures the participant reference and the checkin and checkout times.

Any participant can have many checkins. A checkin record can only have one participant.

The code as it exists now does everything I want except it displays every checkin record for the participant. I only want to display the last (most recent) record for the participant.

How can I construct the queryset to include the participant information and include only the last checkin record for the participant?

Thank you in advance. You are appreciated.

Models

class AdultParticipant(models.Model):

    first_name = models.CharField(max_length=50)
    middle_initial = models.CharField(max_length=50, blank=True)



class MinorParticipant(models.Model):

    first_name = models.CharField(max_length=50)
    middle_initial = models.CharField(max_length=50, blank=True)
    parent = models.ForeignKey(
        WaiverAdult, on_delete=models.CASCADE, related_name='minor_of_adult')



class CheckIn(models.Model):

    adult = models.ForeignKey(
        AdultParticipant, on_delete=models.CASCADE, blank=True, null=True, related_name='adult_checkin')

    minor = models.ForeignKey(
        MinorParticipant, on_delete=models.CASCADE, blank=True, null=True, related_name='minor_checkin')

    checkin = models.DateTimeField(blank=True, null=True)
    checkout = models.DateTimeField(blank=True, null=True)


View

class WaiverListView(ListView):
    
    participants_and_checkins = AdultParticipant.objects.all().order_by('created')

    queryset = participants_and_checkins

    context_object_name = "the_list"

    template_name = 'list.html'

Template


{% for adult in the_list %}

  <tr>                         
   <td >

     {% for c in adult.adult_checkin.all|dictsortreversed:"checkin" %} 
               In: {{ c.checkin }}</br>
               Out: {{c.checkout}}</br>
     {% endfor %}
                         
   </td>
   <td>{{adult.last_name}}</td>
   <td>{{adult.first_name}}</td>
  </tr>


     {% for child in adult.minor_of_adult.all %} 
       <tr>
         <td >

           {% for c in child.minor_checkin.all|dictsortreversed:"checkin" %} 
               In: {{ c.checkin }}</br>
               Out: {{c.checkout}}
           {% endfor %}

         </td>
         <td>{{child.last_name}}</td>
         <td>{{child.first_name}}</td>
       </tr>
    {% endfor %}


{% endfor %}

CodePudding user response:

in your template change checkin.all to checkin.last.

Also you can get rid of the for loop as well,like

In: {{ child.minor_checkin.last.checkin }}</br>
Out: {{child.minor_checkin.last.checkout}}
  • Related