0

テストの試行回数を test_id でグループ化し、各テストの平均を取得する Django クエリがあります。

test_attempts テーブルには、特定のテストでユーザーが行った各テストの試行が記録されます。テストごとの平均試行回数を知りたい

これが私のクエリです:

average = TestAttempts.objects.values('test_id').annotate(Avg(Count('test_id'))).filter(user_id=id)

次のエラーが表示されます: 'Count' オブジェクトには属性 'split' がありません

生の SQL を書かずにこれを処理する方法はありますか?

更新: ここに TestAtttemt モデルがあります

class TestAttempts(models.Model):
id = models.IntegerField(primary_key=True)
user_id = models.IntegerField()
test_id = models.IntegerField()
test_grade = models.DecimalField(max_digits=6, decimal_places=1)
grade_date_time = models.DateTimeField()
start_time = models.DateTimeField()
seconds_taken = models.IntegerField()
taking_for_ce_credit = models.IntegerField()
ip_address = models.CharField(max_length=25L)
grade_points = models.DecimalField(null=True, max_digits=4, decimal_places=1, blank=True)
passing_percentage = models.IntegerField(null=True, blank=True)
passed = models.IntegerField()
class Meta:
    db_table = 'test_attempts'
4

1 に答える 1

0

単一の数値、つまりすべてのテストの平均試行回数が必要ですか? テストモデルはありますか?

これは次のように機能します。

average = (Test.objects.filter(testattempt__user_id=id)
             .annotate(c=Count('testattempt'))
             .aggregate(a=Avg('c'))['a'])

 

TestAttempt → Test 関係がなく、 test_id フィールドしかない場合、これは機能するはずです:

average  = (TestAttempt.objects.filter(user_id=2)
                               .values('test_id')
                               .annotate(c=Count('pk'))
                               .aggregate(a=Avg('c')))

しかし、sqliteではうまくいきません。手元に適切なデータベースがありません。

于 2013-04-07T20:33:04.523 に答える