python - 如何通过在 django 中加入两个表来获取数据?
问题描述
寻求帮助时卡住了,我是 python 和 django 的新手。有两个付款对应于一个订单,一个 COLLECTION 和多个 TRANSFER,我需要与一个方向为 COLLECTION 的订单相对应的付款,但尚未转移,以便我可以针对该订单启动 TRANSFER
模型.py
class Orders(models.Model):
id= models.AutoField(primary_key=True)
payment_gateway_code = models.CharField(max_length=20,choices=[('PAYTM','PAYTM')])
is_active = models.BooleanField(default=True)
class Payments(models.Model):
id = models.AutoField(primary_key=True)
orders = models.ForeignKey(Orders, on_delete=models.CASCADE)
direction = models.CharField(max_length=20,choices=[('COLLECTION','COLLECTION'),
('TRANSFER','TRANSFER')])
settlement_status = models.CharField(max_length=50,blank=True, null=True,choices=[('YES','YES'),
('NO','NO')])
is_active = models.BooleanField(default=True)
qualified_orders = Orders.objects.filter(payment_gateway_code='CASHFREE',
Exists(Payments.objects.filter(order=OuterRef('pk'), direction='COLLECTION',
settlement_status='YES')), ~Exists(Payments.objects.filter(order=OuterRef('pk'),
direction='TRANSFER')))
但上面的查询不起作用
解决方案
您可以按如下方式使用views.py
模型.py
class Orders(models.Model):
id= models.AutoField(primary_key=True)
payment_gateway_code = models.CharField(max_length=20,choices=[('PAYTM','PAYTM')])
is_active = models.BooleanField(default=True)
class Payments(models.Model):
id = models.AutoField(primary_key=True)
orders = models.ForeignKey(Orders, on_delete=models.CASCADE)
direction = models.CharField(max_length=20,related_name="direction",choices=[('COLLECTION','COLLECTION'),
('TRANSFER','TRANSFER')])
settlement_status = models.CharField(max_length=50,blank=True, null=True,choices=[('YES','YES'),
('NO','NO')])
is_active = models.BooleanField(default=True)
视图.py
from App.models import orders, payments
#in case if you need objects of order this is for that
def orderfunc():
order = Orders.objects.all()
def paymentfunc():
payment = Payment.objects.all()
# from here you can check for what record you want using conditional operator
#if direction == COLLECTION:
#then do what you need
推荐阅读
- java - 以编程方式部署 Flowable 工作流
- google-app-engine - 使用 Go on App Engine Standard 调用系统包
- r - 如果我只有 X,Y 坐标,如何计算 R 中多边形的面积?
- amazon-web-services - Amazon Managed Blockchain 和 Quantum Ledger Database (QLDB) 之间的主要区别?
- reactjs - 将数组作为道具传递给组件时出错
- wordpress - Wordpress 缓存 - 将 W3 Total 缓存与 CloudFront 结合使用与直接使用 CloudFront
- java - 嵌套 For 循环转换为 Lambda 和地图比较
- python - 如何在 Python 中创建一系列矩阵(使用 pandas 和 Gurobi)
- spring - Spring Actuator Metrics 生成日志
- django - 如何在当前模型的获取绝对 URL django 中获取另一个模型的 pk 值