我想根据订阅类型生成一个成员数。我正在寻找的是一群军官,士兵和平民(我正在创造一个游戏)。成员表注册实际成员,而订阅表注册他们拥有的订阅类型。由于每个订阅都是1年,因此每个成员会产生多个订阅记录。
我正在寻找的输出是
Enlisted Officer Civilian 5 12 32
问题在于记录数。我试过了:
models.Subscription.objects.values('Subscription_Type').annotate(grouping=Count('Subscription_Type'))
但是如果我有一个新订阅的会员(比如说12/1/2013),那么重新订阅(比如说12/1/2014),就会显示为两个。我需要一个计数,因为它只有一个成员。
我有以下表格:
class Member(models.Model):
id = models.AutoField(primary_key=True)
member_id = models.CharField(unique=True, max_length=30)
first_name = models.CharField(max_length=30)
middle_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.CharField(max_length=50)
home_phone = PhoneNumberField(blank=True)
cell_number = PhoneNumberField(blank=True)
fax_number = PhoneNumberField(blank=True)
Cell_phone = models.CharField(max_length=20)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
User = models.ForeignKey(User, blank=True, null=True)
def __str__(self): # __unicode__ on Python 2
return self.first_name + " " + self.last_name
class Subscription(models.Model):
SType = (
('C', 'Civilian'),
('E', 'Enlisted'),
('O', 'Officer'),
)
Subscription_Type = models.CharField(max_length=1, choices=SType)
Lifetime = models.BooleanField(default=False)
Member = models.ForeignKey(Member)
Modified = models.DateField()
def __str__(self):
return self.Member.first_name + " " + self.Member.last_name + ": " + self.Subscription_Type + "; last modified: " + self.Modified.strftime('%Y-%m-%d')