从 2 个表中召回的问题,按 id,客户存入了多少,在表中
Posted
技术标签:
【中文标题】从 2 个表中召回的问题,按 id,客户存入了多少,在表中【英文标题】:A problem with recalling from 2 table, by id, how much customer deposited, in table 【发布时间】:2022-01-22 18:57:24 【问题描述】:我正在编写一个 crm,供我自己学习。
我有一个问题,因为我有 1 个表“客户”,而在另一个表中我存储了该客户的存款,我需要在表中的页面上显示客户存款,过滤器“状态 = 4 ",我尝试将其作为 annoate 进行,但无法调出相应的 id。
模型.py
class Client(models.Model):
def __str__(self):
return self.name + " " + self.lastname
class Meta:
verbose_name = 'Client'
verbose_name_plural = 'Client'
name = models.CharField(max_length=64)
lastname = models.CharField(max_length=64)
phone = models.DecimalField(max_digits=20, decimal_places=0)
email = models.EmailField(max_length=64, null=True)
salesman = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='salesman')
retention = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='retention')
reg_time = models.DateField(blank=True, null=True)
class Operation(models.Model):
def __str__(self):
return self.client.name + ' ' + self.client.lastname + ' Cash: ' + str(self.cash)
client = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='client')
cash = models.DecimalField(max_digits=12, decimal_places=2)
date = models.DateField(blank=True, null=True)
bank = models.ForeignKey(Bank, on_delete=models.CASCADE, related_name='bank')
status = models.ForeignKey(Status, on_delete=models.CASCADE, related_name='stat')
who = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='ftd_employees')
class Meta:
verbose_name = 'Operations'
verbose_name_plural = 'Operations'
client_list.html
<tbody>
% for c in clients_list %
<tr>
<td> c.id </td>
<td> c.name c.lastname </td>
<td> c.email </td>
<td> c.phone </td>
<td> c.salesman.name c.salesman.lastname </td>
<td> c.retention.name c.retention.lastname </td>
<td> c.phone </td>
<td> c.reg_time </td>
<td> c.depo </td>
% endfor %
</tr>
</tbody>
views.py
@property
def client_ftd(self):
depo = Client.objects.annotate(Operation=Count("kto__id")).annotate(totals=Sum("kto__cash")).filter(kto__status=4)
return depo
def client_list(request):
employee = Employee.objects.all()
clients_list = Client.objects.all()
return render(request, 'client_list.html', 'clients_list': clients_list,
'employee': employee, 'client_ftd': client_ftd)
【问题讨论】:
【参考方案1】:我假设您正在寻找这个简单的查询:
clients_list = Client.objects.annotate(depo=Sum('operation__cash', filter=Q(operation__status=4)))
看看你的代码,我认为你真的需要了解更多:
properties(@property 装饰器旨在用于方法的类内部;为什么将其放在视图中??), 聚合查询(什么是“kto”,annotate(Operation=Count("kto__id")) 有什么意义??)??【讨论】:
【参考方案2】:问题是你的clients_list = Client.objects.all()
。
对于解决方案,您需要将.all
替换为.annotate
。随后,您必须在您的 depo 中打开一个括号,因此 (depo=Sum('operation__cash', filter=Q(operation__status=4)))
。如您所见,使用了"Status = 4"
过滤器。
这样您将从 2 个表中回调,按 id,客户存入的金额,在表中 :)
@property
def client_ftd(self):
depo = Client.objects.annotate(Operation=Count("kto__id")).annotate(totals=Sum("kto__cash")).filter(kto__status=4)
return depo
def client_list(request):
employee = Employee.objects.all()
clients_list = Client.objects.annotate(depo=Sum('operation__cash', filter=Q(operation__status=4))) #update
return render(request, 'client_list.html', 'clients_list': clients_list,
'employee': employee, 'client_ftd': client_ftd)
【讨论】:
【参考方案3】:我认为您可以使用view.py
中的此查询优化您的数据库查询:
Operation.objects.filter(status__id = 4).annotate(depo = Sum('cash')).values(
'client__id',
'client__name',
'client__email',
'client__phone',
'client__salesman__name',
'client__salesman_lastname',
'client__relation__name',
'client__relation.lastname',
'client__reg_time',
)
检查它是否有效。它应该只返回您需要的字段并更有效地工作。 避免在 Python 中使用循环。数据库效率更高。
【讨论】:
以上是关于从 2 个表中召回的问题,按 id,客户存入了多少,在表中的主要内容,如果未能解决你的问题,请参考以下文章