Django查询未返回所有记录

时间:2018-08-09 08:33:41

标签: django django-models django-filter

我正在尝试做一个简单的查询,但是我不明白为什么在示例2中我只得到1行:

我正在尝试获取externalID = 45454的所有行 通过这样做:

context['report'] = Report.objects.filter(externalID =  self.kwargs.get('reportID'))

我可以在调试中看到

SELECT "data_app_report"."id", "data_app_report"."main_contact_id", "data_app_report"."company_id", "data_app_report"."created", "data_app_report"."externalID", "data_app_report"."success", "data_app_report"."amountrequested" FROM "data_app_report" WHERE "data_app_report"."externalID" = '''45454'''

我的桌子看起来像

enter image description here

我想我在查询中做错了什么,但我只是不知道是什么,我认为拥有过滤器将使我能够获取所有记录

这是2种型号:

class Report(models.Model):
    main_contact = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    company = models.ForeignKey(CompanyDetail, on_delete=models.CASCADE, related_name='companyreport', default=1)
    created = models.DateTimeField(auto_now_add=True)
    externalID = models.CharField(default='0000000', max_length=35, unique = False, editable=False) #ONE PERSON CAN HAVE MULTIPLE BANKING IN THE SAME OPP, THEN THE REF IS THE SAME
    success = models.BooleanField(default = True)
    amountrequested = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True)

class ReportURL(models.Model):
    report = models.ForeignKey(Report, on_delete=models.CASCADE, related_name='reporturls')
    file_type = models.CharField(max_length=5, unique = False, default='html')
    report_url = models.URLField(unique=True)

这是我的观点

class ExtAccView(TemplateView):
    template_name = 'data_app/ext_access.html'
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['reportID'] = self.kwargs.get('reportID')
        context['access'] = self.kwargs.get('access')

        #GET THE COMPANY INFORMATION
        try:
            context['company'] = CompanyDetail.objects.get(companyExternalKey =  self.kwargs.get('access'))
        except Exception as e:
            context['message'] = 'The company doesnt match the values you submitted ' + self.kwargs.get('access')
            return context

        #GET THE REPORT AND WE NEED TO DOUBLE CHECK IF THE REPORT COMPANY IS THE SAME AS ABOVE
        try:
            # context['report'] = Report.objects.get(externalID =  self.kwargs.get('reportID'))
            context['report'] = Report.objects.filter(externalID =  self.kwargs.get('reportID'))
        except Exception as e:
            context['message'] = 'No reports found with that reference: ' + self.kwargs.get('reportID')
            return context

        return context

这是我的模板(ext_access.html)

{% for r in reportURL %}
    {{ r.report_url }}
{% endfor %}

0 个答案:

没有答案