我有這樣的查詢:如何將此MySQL查詢轉換爲Django查詢?
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]
models.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()
我怎麼能這樣做?由於
這個問題是您創建 – catherine 2013-03-15 12:07:47
呀同前面的問題,但我並沒有得到預期的答案。 – pynovice 2013-03-15 12:09:20
你知道你也可以回退到原始的SQL https://docs.djangoproject.com/en/dev/topics/db/sql/ – 2013-03-15 12:09:37