从 django 外键连接模型查询查询集

时间:2021-04-14 06:18:43

标签: django

我有一个如下所示的模型:

class Flow(models.Model):

    flow_type_choices = (('Static', 'Static'), ('Transit', 'Transit'), ('Circuit', 'Circuit'), ('Close', 'Close'))

    flow_name = models.CharField(max_length=500, default=0)
    flow_info = models.CharField(max_length=500, default=0)

    flow_type = models.CharField(max_length=500, default=0, choices=flow_type_choices)
    flow_days = models.IntegerField(default=0)
    sender_client = models.ForeignKey(Client, on_delete=models.CASCADE)
    receiver_client = models.ForeignKey(ReceiverClient, on_delete=models.CASCADE)

我正在尝试将 querysetreceiver_client 附加到特定的 sender client

为此,我尝试了以下方法:

         items = list(Flow.objects.filter(sender_client=request.user.pk).
                 values_list('receiver_client', flat=True).distinct())
    print("items", items)
    rc = ReceiverClient.objects.filter(pk__in = items)

我得到:

items <QuerySet [11, 8, 7, 18, 4, 6, 3]>

如何获取接收方客户端的查询集而不是 pk

1 个答案:

答案 0 :(得分:4)

您可以通过跟踪从 <ul class="list-with-arrow"> <!-- outputted a list item before any of the other content --> <li> <hr class="list-with-arrow__bottom-line"> <div class="list-with-arrow__container"> <a class="list-with-arrow__label-container href="link"> <!--- swapped the inner list item to a div --> <div data-index="1"> <h5 class="list-with-arrow__title">Title</h5> </div> <div> <!-- notice I added focusable="false" and aria-hidden="true" to the SVG to avoid it being announced as an image without a description. --> <!-- if this does contain useful information you should instead add a `<title>` element that describes the image --> <svg focusable="false" aria-hidden="true" width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> <path fill-rule="evenodd" clip-rule="evenodd" d="M8.99999 4.93933L15.5303 11.4697C15.8232 11.7626 15.8232 12.2374 15.5303 12.5303L8.99999 19.0607L7.93933 18L13.9393 12L7.93933 5.99999L8.99999 4.93933Z" fill="black"/> </svg> </div> </a> </div> </li> [...further items] </ul> ReceiverClient 的关系,在单个查询中获取这些数据

Client