【问题标题】:Django DISTINCT issue related to database与数据库相关的 Django DISTINCT 问题
【发布时间】:2018-05-24 16:14:08
【问题描述】:

在我的数据库中,我的 report_id 为 100,但它有两个与我的应用程序无关的 report_id 部分。我能够在下面的示例中使用 .distinct() 。有一次效果很好。

在下面的示例中,我的数据在我的表单中正确返回,但我需要使用第一个示例中不存在的 ID 发布数据。

reportaccess = QvReportList.objects.filter(report_id__in= reportIds).values_list('report_name_sc', flat = True).distinct()

我必须将我的查询集更改为以下内容,以允许 report_id 发布到下一个视图。

currreportaccess = QvReportList.objects.filter(report_id__in= reportIds).distinct()

我的问题是因为我不再有带有单个字段的 values_list 。它以两行不同的形式返回。

我的 HTML 定义如下:

{% for app in currreportaccess %}

              <li> <input type="checkbox" name="current_report" value ="{{app.report_id}}" >  {{ app.report_name_sc }}</li>

          {% endfor %}

在这种情况下是否有可能得到一个独特的?如果是这样,我应该怎么做?

我已经为表 QVReportList 添加了我的模型。

class QvReportList(models.Model):
    qv_dept_id = models.CharField(db_column='QV_Dept_ID', max_length=100)  # Field name made lowercase.
    report_id = models.CharField(db_column='Report_ID',primary_key=True, max_length = 100, serialize=False)  # Field name made lowercase.
    report_name = models.CharField(db_column='Report_Name', max_length=255, blank=True, null=True)  # Field name made lowercase.
    report_name_sc = models.CharField(db_column='Report_Name_SC', max_length=255, blank=True, null=True)  # Field name made lowercase.
    qv_filename = models.CharField(db_column='QV_FileName', max_length=255, blank=True, null=True)  # Field name made lowercase.
    report_access = models.CharField(db_column='Report_Access', max_length=20, blank=True, null=True)  # Field name made lowercase.
    report_group_id = models.IntegerField(db_column='Report_Group_ID', blank=True, null=True)  # Field name made lowercase.
    report_sub_group_id = models.IntegerField(db_column='Report_Sub_Group_ID', blank=True, null=True)  # Field name made lowercase.
    load_date = models.DateTimeField(db_column='Load_Date', blank=True, null=True)  # Field name made lowercase.
    approver_fname = models.CharField(db_column='Approver_FName', max_length=255, blank=True, null=True)  # Field name made lowercase.
    approver_lname = models.CharField(db_column='Approver_LName', max_length=255, blank=True, null=True)  # Field name made lowercase.
    approver_ntname = models.CharField(db_column='Approver_NTName', max_length=255, blank=True, null=True)  # Field name made lowercase.
    beg_date = models.DateTimeField(db_column='Beg_Date', blank=True, null=True)  # Field name made lowercase.
    end_date = models.DateTimeField(db_column='End_Date', blank=True, null=True)  # Field name made lowercase.
    active = models.IntegerField(db_column='Active', blank=True, null=True)  # Field name made lowercase.
    approval_id = models.IntegerField(db_column='Approval_ID', blank=True, null=True)  # Field name made lowercase.
    role_based_id = models.IntegerField(db_column='Role_Based_ID', blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'QV_Report_List'

问题在于报告 ID '100' 的字段 report_access 有 report_access 的 Summary 和 Patient 值。

我不能只在字段名称上调用 distinct,因为我需要使用 SQL 服务器并且不能切换到 posgress。我收到以下错误消息。

DISTINCT ON fields is not supported by this database backend

【问题讨论】:

    标签: django


    【解决方案1】:

    你可以像这样使用 .values() insted of .values_list():

    QvReportList.objects.filter(
        report_id__in= reportIds
    ).values('report_id', 'report_name_sc'
    ).distinct()
    

    values() 返回 dict(或类似的东西),在你的模板中你可以得到你写的一样。

    【讨论】:

    • 这在当前视图上解决了它。但是,当我调用 currentcheckedlist = request.POST.getlist('current_report').distinct() 时,它仍然有两行。如果我尝试向它添加不同的,那么我会得到“列表”对象没有属性“不同”。我能够通过创建以下新变量来解决它。 reportaccess = QvReportList.objects.filter(report_id__in= currentcheckedlist).values('report_id','report_name_sc').distinct() 与您的答案具有相同的逻辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 2020-03-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    相关资源
    最近更新 更多