I am trying to replicate Django's Each publisher, each with a count of books as a "num_books" attribute using the following models:
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')
...
From what I can tell, this should allow me to get a count of requests for each type using this:
Models.RequestTypes.objects.annotate(num_requests=Count('requests'))
But that just gives a FieldError
, Cannot resolve keyword 'requests' into field.
Any ideas what I've missed?
Models.RequestTypes.objects.annotate(num_requests=Count('requests_set'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.