我的django模型中有以下架构:
class CompanyModel(Model):
name = CharField('name', max_length=75, db_index=True)
logo = ImageField('logo', upload_to='company_logo/', blank=True,)
class OfficeModel(Model):
company = ForeignKey(CompanyModel, related_name='office_company', db_index=True)
address1 = CharField('address 1', max_length=50,)
address2 = CharField('address 2', max_length=50, blank=True)
city = CharField('city', max_length=50, db_index=True)
state = CharField('state', max_length=2, choices=STATE_CHOICES, db_index=True)
我想获得那里有公司数量的州名单。问题是我使用MySQL,不支持某个字段,并且公司可以在一个州内拥有许多办公室。
这是我的fiers方法:
office_query.values('state').annotate(count=Count('id')).order_by('-count')
有什么想法吗?
谢谢!