Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python/django计算模型字段实例的唯一(或不同)值_Python_Sql_Django_Analytics_Dashboard - Fatal编程技术网

使用python/django计算模型字段实例的唯一(或不同)值

使用python/django计算模型字段实例的唯一(或不同)值,python,sql,django,analytics,dashboard,Python,Sql,Django,Analytics,Dashboard,正在尝试显示数据库中存在的字段的唯一实例数。在下面的模型中,“uid”是一个用户id#,用户可以在同一个“uid”下在数据库上创建多个记录。因此,如果我计算“uid”的数量,我只需获得数据库中创建的所有记录的数量,但我正在尝试获取数据库中存在的唯一“uid”的数量。我一直在处理.count()、count和.distinct(),运气不佳。我对distinct=True选项也很困惑。这是为了向分析仪表板提供数据,因此我想计算数据库中存在的不同用户的数量 型号.py class Session(m

正在尝试显示数据库中存在的字段的唯一实例数。在下面的模型中,“uid”是一个用户id#,用户可以在同一个“uid”下在数据库上创建多个记录。因此,如果我计算“uid”的数量,我只需获得数据库中创建的所有记录的数量,但我正在尝试获取数据库中存在的唯一“uid”的数量。我一直在处理.count()、count和.distinct(),运气不佳。我对distinct=True选项也很困惑。这是为了向分析仪表板提供数据,因此我想计算数据库中存在的不同用户的数量

型号.py

class Session(models.Model):
    uid = models.CharField(max_length=50, blank=True)
    cid = models.CharField(max_length=50, blank=True)  # switched from eid
    client = models.CharField(max_length=50, blank=True)
class DashboardListView(LoginRequiredMixin, ListView):
    model = Session
    template_name = 'blog/dashboard.html'

    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Session.objects.filter(client=user).order_by('-session_date')

    def get_context_data(self, **kwargs):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        context = super().get_context_data(**kwargs)
        context['total_records'] = Session.objects.filter(client=user).count()  # count function
        return context
视图.py

class Session(models.Model):
    uid = models.CharField(max_length=50, blank=True)
    cid = models.CharField(max_length=50, blank=True)  # switched from eid
    client = models.CharField(max_length=50, blank=True)
class DashboardListView(LoginRequiredMixin, ListView):
    model = Session
    template_name = 'blog/dashboard.html'

    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Session.objects.filter(client=user).order_by('-session_date')

    def get_context_data(self, **kwargs):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        context = super().get_context_data(**kwargs)
        context['total_records'] = Session.objects.filter(client=user).count()  # count function
        return context
html标记

{{ total_records }}
来自模型会话的sqlite数据库记录示例

{{ total_records }}
uid | cid |客户端

001 | abc | DummyCo

001 | abc | DummyCo

001 | xyz | DummyCo

002 | xyz | DummyCo

002 | abc | DummyCo

003 | abc | DummyCo

因此,这个的期望输出是:

不同uid的:3

不同的cid:2

(在这种情况下,客户端将始终相同。)


只是想澄清一下:我不需要计算“001”被列为“uid”的次数,我需要知道数据库中存在多少不同的“uid”。

正如@sabiwara所说,这个问题以前已经得到了回答。我只是想把我的最后一行代码放在这里,因为我有一个更简单的用例,它可能会在将来帮助别人

这是adjusted views.py部分,我在其中添加了新的代码行

    def get_context_data(self, **kwargs):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        context = super().get_context_data(**kwargs)
        session_list = list(context['sessions'])  # evaluates the query
        context['total_users'] = Session.objects.filter(client=user).values('uid').distinct().count() # new code line
        context['sessions'] = session_list
        return context
**html标记**

{{ total_users }}

你查过了吗?您是否尝试过使用
distinct
count
链接,例如
Sessions.objects.values('uid').distinct().count()
。谢谢,成功了。我在上面看到了这个问题,但我很难将它应用到我的使用中,因为那个人正在处理两个模型并使用
\uuuu
。非常感谢。