Django反向外键FieldError

时间:2015-09-15 17:05:42

标签: python django django-models django-queryset

我正在尝试使用以下模型复制Django' Each publisher, each with a count of books as a "num_books" attribute

class Requests(models.Model):
    request_id      = models.AutoField(db_column='RequestId', primary_key=True)
    request_type    = models.ForeignKey('RequestTypes', db_column='RequestTypeId')
    ...
class RequestTypes(models.Model):
    request_type_id = models.CharField(db_column='RequestTypeId', primary_key=True)
    request_type_title           = models.CharField(db_column='Title')
    ...

据我所知,这应该允许我使用这个来计算每种类型的请求:

Models.RequestTypes.objects.annotate(num_requests=Count('requests'))

但这只是FieldError,无法解析关键字'请求'进入田野。

我错过了哪些想法?

1 个答案:

答案 0 :(得分:0)

Models.RequestTypes.objects.annotate(num_requests=Count('requests_set'))