简体   繁体   English

Django Queryset 包含和过滤相关数据

[英]Django Queryset to include and filter related data

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.有两种类型的参与者,它们在两个表/模型中表示:AdultParticipant 和 MinorParticipant。 MinorParticipants have a foreign key relationship to the ParentParticipant. MinorParticipants 与 ParentParticipant 有外键关系。

Checkin information (checkin and checkout) is stored in a single table irregardless of whether the checkin data refers to AdultParticipant or the MinorParticipant.签入信息(签入和签出)存储在单个表中,无论签入数据是指 AdultParticipant 还是 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 class="fs-6">

     {% 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 class="fs-6">

           {% 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 %}

in your template change checkin.all to checkin.last .在您的模板checkin.all checkin.all 更改为checkin.last

Also you can get rid of the for loop as well,like你也可以摆脱 for 循环,比如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM