0

我有一个这样的查询:

select files_id, count(DISTINCT shared_user_id) FROM fileupload_share GROUP BY files_id;

我想在 Django 查询中进行转换。像这样尝试:

count = Share.objects.filter(users_id=request.user.id).values_list('shared_user_id').distinct().count()

但没有按要求的输出给我一个组。我希望输出在列表中返回,例如

count = [1, 2, 3, 1]

模型.py:

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()

我怎样才能做到这一点?谢谢

4

1 回答 1

1

尝试这样的事情:

files = File.objects.filter(users_id=request.user.id).annotate(count=Count('share__shared_user_id')

那么你应该有

for f in files:
    print f.file_name, f.count
于 2013-03-15T12:42:10.103 回答