0

我有一个这样的MYSQL查询:

shared_file = File.objects.filter(id__in= Share.objects.filter(user_id = log_id).values_list('file', flat=True)).annotate(count=Count('share__shared_user_id')).distinct()

我正在尝试获取所有文件信息以及共享文件的人数。我希望 Share 模型中的 shared_user_id 为DISTINCT. 我的两个模型文件和共享是:

class File(models.Model):
    user = models.ForeignKey(User)
    file_name = models.CharField(max_length=100)
    type = models.CharField(max_length=10)              #File extension
    source = models.CharField(max_length=100)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    size = models.CharField(max_length=50)              #Stored in humansize
    flag = models.CharField(max_length=1)               #Flag to show deleted files
    flag_overview = models.CharField(max_length=1)      #Flag to remove file from 

class Share(models.Model):
    user = models.ForeignKey(User)
    file = models.ForeignKey(File)
    shared_user_id = models.IntegerField()
    shared_date = models.DateTimeField()

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

4

1 回答 1

0

尝试

shared_file = File.objects.filter(id__in =
                                  Share.objects.filter(user_id = log_id)
                                               .values_list('file', flat=True))
                          .annotate(count=Count('share__shared_user_id',
                                                distinct=True))
于 2013-06-07T13:26:57.073 回答