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

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


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)

我可以涉及Profile和User,但我试图涉及Profile到DailyReports上agent_code,然后涉及到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')}

...

但我也想从得到的信息Profile基础上agent_code,再对相关的User基础上订立的相关信息agent_code之间的Profile和DailyReports,这样,我的输出如下所示:


{'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} 


慕妹3146593
浏览 247回答 1
1回答

繁星点点滴滴

如果一个配置文件将链接到多个 DailyReports 最佳设置将必须是class Profile(models.Model):&nbsp; &nbsp; COORDINATOR = 1&nbsp; &nbsp; LEADER = 2&nbsp; &nbsp; ADMIN = 3&nbsp; &nbsp; ROLE_CHOICES = (&nbsp; &nbsp; &nbsp; &nbsp; (COORDINATOR, 'Coordinator'),&nbsp; &nbsp; &nbsp; &nbsp; (LEADER, 'Leader'),&nbsp; &nbsp; &nbsp; &nbsp; (ADMIN, 'Admin'),&nbsp; &nbsp; )&nbsp; &nbsp; user = models.OneToOneField(User, on_delete=models.CASCADE)&nbsp; &nbsp; team = models.ForeignKey(Team, on_delete=models.PROTECT,null=True)&nbsp; &nbsp; role = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, null=True, blank=True)&nbsp; &nbsp; # 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):&nbsp; &nbsp; profile = models.ForeignKey('Profile', related_name='daily_reports')&nbsp; &nbsp; agent_code = models.CharField(max_length=15, blank=True, null=True)&nbsp; &nbsp; product = models.CharField(max_length=15)&nbsp; &nbsp; num_free = models.IntegerField(blank=True, null=True)&nbsp; &nbsp; apps_submitted = models.IntegerField(blank=True, null=True)&nbsp; &nbsp; apps_activated = models.IntegerField(blank=True, null=True)&nbsp; &nbsp; prem_submitted = models.DecimalField(max_digits=20, decimal_places=2,blank=True, null=True)&nbsp; &nbsp; 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 = (&nbsp; &nbsp; profile.daily_reports.filter(product__in=['LT15', 'LT121'])&nbsp; &nbsp; &nbsp; &nbsp; .annotate(premium=Sum('prem_submitted'))&nbsp; &nbsp; &nbsp; &nbsp; .values_list('agent_code', 'premium', 'profile__first_name', 'profile__last_name', 'profile__user_id', 'profile__role', named=True)&nbsp; &nbsp; &nbsp; &nbsp; .order_by('-premium'))results = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;'agent_code': report.agent_code,&nbsp; &nbsp; &nbsp; &nbsp;'premium': report.premium&nbsp; &nbsp; &nbsp; &nbsp;'first_name': report.profile__first_name,&nbsp; &nbsp; &nbsp; &nbsp;'last_name': report.profile__last_name,&nbsp; &nbsp; &nbsp; &nbsp;'user_id': report.profile__user_id,&nbsp; &nbsp; &nbsp; &nbsp;'role': report.profile__role&nbsp; &nbsp; } for report in qs]## {'agent_code': 'ABC123', 'premium': Decimal('479872.55'), ...}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python