1 回答

TA贡献1803条经验 获得超3个赞
如果一个配置文件将链接到多个 DailyReports 最佳设置将必须是
class Profile(models.Model):
COORDINATOR = 1
LEADER = 2
ADMIN = 3
ROLE_CHOICES = (
(COORDINATOR, 'Coordinator'),
(LEADER, 'Leader'),
(ADMIN, 'Admin'),
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
team = models.ForeignKey(Team, on_delete=models.PROTECT,null=True)
role = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, null=True, blank=True)
# agent_code = models.CharField(max_length=15, null=True, blank=True) # <-- Remove the agent code from the profile model.
DailyReports 与 Profile 是多对一的关系。
class DailyReports(models.Model):
profile = models.ForeignKey('Profile', related_name='daily_reports')
agent_code = models.CharField(max_length=15, blank=True, null=True)
product = models.CharField(max_length=15)
num_free = models.IntegerField(blank=True, null=True)
apps_submitted = models.IntegerField(blank=True, null=True)
apps_activated = models.IntegerField(blank=True, null=True)
prem_submitted = models.DecimalField(max_digits=20, decimal_places=2,blank=True, null=True)
date = models.DateField(auto_now=False,auto_now_add=False,null=True,blank=True)
获取配置文件的 DailyReports 列表
profile = Profile.objects.prefetch_related('daily_reports').first()
profile.daily_reports.all()
通过配置文件报告查询
qs = (
profile.daily_reports.filter(product__in=['LT15', 'LT121'])
.annotate(premium=Sum('prem_submitted'))
.values_list('agent_code', 'premium', 'profile__first_name', 'profile__last_name', 'profile__user_id', 'profile__role', named=True)
.order_by('-premium')
)
results = [
{
'agent_code': report.agent_code,
'premium': report.premium
'first_name': report.profile__first_name,
'last_name': report.profile__last_name,
'user_id': report.profile__user_id,
'role': report.profile__role
} for report in qs
]
## {'agent_code': 'ABC123', 'premium': Decimal('479872.55'), ...}
添加回答
举报