【问题标题】:Count number of shared users to the given file in django?计算django中给定文件的共享用户数?
【发布时间】:2013-03-15 07:25:06
【问题描述】:

我有一个这样的名为 share 的表:

+----+----------+----------+----------------+----------------------------------+
| id | users_id | files_id | shared_user_id | shared_date                      |
+----+----------+----------+----------------+----------------------------------+
|  2 |        1 |        2 |              2 | 2013-03-13 20:33:36.766030+00:00 |
|  4 |        1 |        4 |              2 | 2013-03-13 20:33:36.766030+00:00 |
|  5 |        1 |        5 |              2 | 2013-03-13 20:33:36.766030+00:00 |
|  6 |        1 |        6 |              2 | 2013-03-13 20:33:36.766030+00:00 |
|  8 |        1 |        2 |              2 | 2013-03-14 16:15:29.580095+00:00 |
| 10 |        1 |        4 |              2 | 2013-03-14 16:15:29.580095+00:00 |
| 11 |        1 |        5 |              2 | 2013-03-14 16:15:29.580095+00:00 |
| 12 |        1 |        6 |              2 | 2013-03-14 16:15:29.580095+00:00 |
| 13 |        1 |        7 |              2 | 2013-03-14 16:15:29.580095+00:00 |
| 14 |        1 |        8 |              2 | 2013-03-14 16:15:29.580095+00:00 |
| 15 |        1 |        9 |              2 | 2013-03-14 16:15:29.580095+00:00 |
| 16 |        1 |       12 |              2 | 2013-03-14 16:15:29.580095+00:00 |
| 18 |        1 |        2 |              2 | 2013-03-14 16:17:57.929392+00:00 |
| 20 |        1 |        4 |              2 | 2013-03-14 16:17:57.929392+00:00 |
| 21 |        1 |        5 |              2 | 2013-03-14 16:17:57.929392+00:00 |
| 22 |        1 |        6 |              2 | 2013-03-14 16:17:57.929392+00:00 |
| 23 |        1 |        7 |              2 | 2013-03-14 16:17:57.929392+00:00 |
| 24 |        1 |        8 |              2 | 2013-03-14 16:17:57.929392+00:00 |
| 25 |        1 |        9 |              2 | 2013-03-14 16:17:57.929392+00:00 |
| 26 |        1 |       12 |              2 | 2013-03-14 16:17:57.929392+00:00 |
| 27 |        1 |       12 |              2 | 2013-03-15 15:51:50.344222+00:00 |
| 28 |        1 |        9 |              3 | 2013-03-15 15:58:06.655270+00:00 |
| 29 |        1 |        2 |              2 | 2013-03-15 15:59:58.023322+00:00 |
+----+----------+----------+----------------+----------------------------------+

我想找出每个files_id 共享文件的用户总数。我该怎么做?

试过这个:

Share.objects.filter(users_id=request.user.id).values_list('shared_user_id', flat=True).distinct()

但这仅计算shared_user_id

这是我的模型:

class Share(models.Model):
    users = models.ForeignKey(User)
    files = models.ForeignKey(File)
    shared_user_id = models.IntegerField()
    shared_date = models.TextField()

class File(models.Model):
    users = models.ForeignKey(User)
    file_name = models.CharField(max_length=100)
    type = models.CharField(max_length=10)
    source = models.CharField(max_length=100)
    start_date = models.TextField()
    time_overview = models.CharField(max_length=55)
    end_date = models.TextField()
    duration = models.TextField()
    size_overview = models.IntegerField()
    size = models.TextField()
    flag = models.TextField()
    flag_r = models.TextField()

【问题讨论】:

  • 我不知道如何在 django 中做到这一点,但按子句放了一个分组

标签: mysql django django-queryset


【解决方案1】:

您想使用 Django 的 Aggregate 功能。

使用您的模型,您可以:

counts = Share.objects.all().annotate(num_shares=Count("shared_user_id"))
for count in counts:
    print "%s - %s shares" % (count.files, count.num_shares)

要获取特定文件的计数,假设 file 是表示给定文件的对象:

count = Share.objects.filter(files=file).annotate(num_shares=Count("shared_user_id"))
print count[0].num_count

【讨论】:

  • 什么是file.id?什么是 num_shared?查看我更新的模型问题。
  • 使用您的以下查询我收到以下错误:找不到所需的参数“名称”(位置 1)
  • 查看我更新的问题,其中包含文件模型。谢谢
  • 而且,如果我执行 file = Share.objects.filter(users_id=request.user.id) 我得到子查询返回不止一行。
【解决方案2】:

在mysql中可以用一个简单的查询来计数

SELECT
    files_id,
    count(shared_user_id) AS `Shared`
FROM mytable
GROUP BY files_id
ORDER BY files_id

【讨论】:

  • 还有我为什么要计算 users_id。我要数shared_user_id。
  • @user2032220 如果你想要 django 方式,请从问题中删除 mysql 标签。
猜你喜欢
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多