试图在注释()中获取Count('foo__bar')

时间:2016-11-10 12:39:14

标签: django django-models

我试图让以下行工作:

artists = models.Artists.objects.all().annotate(tot_votes=Count('votes__work')).order_by('-tot_votes')

(即我只想注释与每位艺术家相对应的所有选票的数量。)

但无论何时执行上述行,我都会遇到FieldError错误Cannot resolve keyword 'votes' into field

哪里

class Votes(models.Model):
    work = models.OneToOneField(Works, models.DO_NOTHING, db_column='work')
    user = models.OneToOneField(AuthUser, models.DO_NOTHING, db_column='user')

class Works(models.Model):
    artist = models.ForeignKey(Artists, models.DO_NOTHING, db_column='artist')
    # other irrelevant fields

或者只是表格之间的关系是(投票 - >作品 - >艺术家)

1 个答案:

答案 0 :(得分:1)

我认为你的关系是错误的,不是吗?艺术家与Votes没有任何直接关系,它只对Works有效。注释调用应该是<?php include "init.php";//database connection $sql = "select * from Orders"; $result = mysqli_query($connection,$sql); while($row = mysqli_fetch_assoc($result)){ $userid = $row['userid']; $items = $row['items']; $itemsarray = explode(',', $items); if(!in_array($userid, array_keys($user_item))){ $user_item[$userid] = $itemsarray; } else{ $values = $user_item[$userid]; $arr = array_merge($values,$itemsarray); $user_item[$userid] = $arr; } } echo json_encode($user_item); ?>

(另请注意,Django的常规命名约定是使用单数名称:Vote,Work,Artist。)