基于配置文件字段将三个模型关联在一起

Relate three models together based on profile field

我希望能够将我的用户模型、配置文件模型和一个单独的模型关联起来。我当前的模型结构如下所示:

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)

class DailyReports(models.Model):
    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)

我可以将 ProfileUser 联系起来,但我试图在 agent_code 上将 ProfileDailyReports 联系起来,然后再与User 型号。

所以像下面这样:

    test_query = DailyReports.objects.filter(product__in=['LT15', 'LT121']) \
            .values('agent_code') \
            .annotate(premium=Sum('prem_submitted')) \
            .order_by('-premium') \

我得到了预期的输出:

{'agent_code': 'ABC123', 'premium': Decimal('50015.87')} 
{'agent_code': 'DEF456', 'premium': Decimal('44818.20')} 
{'agent_code': 'GHI789', 'premium': Decimal('35322.35')}
...

但是我也想根据agent_code获取Profile的信息,然后根据agent_code之间的关系获取相关的User信息在 ProfileDailyReports 之间,这样我的输出看起来像:

{'agent_code': 'ABC123', 'premium': Decimal('479872.55'), user.profile.first_name, user.profile.last_name, profile.user_id, profile.role} 
{'agent_code': 'DEF456', 'premium': Decimal('448118.20'), user.profile.first_name, user.profile.last_name, profile.user_id, profile.role} 
{'agent_code': 'GHI789', 'premium': Decimal('356322.35'), user.profile.first_name, user.profile.last_name, profile.user_id, profile.role} 

如果一个配置文件将链接到多个 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。

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'), ...}