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