简体   繁体   中英

How can I fetch data by joining two tables in django?

Looking for help got stuck at a point, I am new to python and django. There ARE two payments corresponding to one order, one COLLECTION and multiple TRANSFER and i need the payment corresponding to an order whose direction is COLLECTION only NOT transfered yet so that i can initiate TRANSFER against that order

models.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')))

But above query is not working

What is OuterRef('pk') ?

First, I'd suggest changing orders to order . Then, the query you're trying to achieve will be something like this (Assuming order_id contains the ID of the order):

Paymen.objects.filter(order_id=order_id, direction="COLLECTION")

You can use views.py for that as follows

Models.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)

views.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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM