0

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

Amit Yadav
  • 29
  • 1
  • 9
  • Does this answer your question? [django select\_related for multiple foreign keys](https://stackoverflow.com/questions/14385445/django-select-related-for-multiple-foreign-keys) – hello Jun 01 '21 at 02:53

2 Answers2

0

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")
Cristian Mora
  • 41
  • 1
  • 3
0

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 
Vishal Pandey
  • 349
  • 1
  • 4
  • 15