0

I'm new in django rest framework and i faced this problem I have two tables Order,Payment I want to get all orders that didn't have payment in the view how can i do this

Models

class Payment(models.Model):

    created_at  = models.DateTimeField(auto_now_add=True)
    updated_at  = models.DateTimeField(auto_now=True)
    amount      =  models.DecimalField(max_digits=7, decimal_places=2)
    invoice     = models.ImageField(upload_to='images')
    is_approved = models.BooleanField()
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    paymentMethod = models.ForeignKey(PaymentMethod, on_delete=models.CASCADE)

class Order (models.Model):
    firstname   = models.CharField(max_length = 20)
    lastname  = models.CharField(max_length = 20)
    emailaddress = models.CharField(max_length = 20)
    phone = models.CharField(max_length = 11)
    discount = models.DecimalField(max_digits=5,blank = True ,null = True,decimal_places=2)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    venture = models.ForeignKey(Venture ,related_name ='ventures' ,on_delete=models.CASCADE)
    salesPerson = models.ForeignKey(SalesPerson,related_name ='salesPerson',blank = True,null = True ,on_delete=models.CASCADE)
    applicationForm = models.OneToOneField(ApplicationForm,blank = True,null = True,on_delete=models.CASCADE)

serializers

class OrderSerializer(serializers.ModelSerializer):
  
    class Meta:
        model = Order
        fields = ['id','firstname','lastname','emailaddress','phone','','product','wave','venture']

class PaymentSerializer(serializers.ModelSerializer):
    

    class Meta:
        model = Payment
        fields = ['created_at','updated_at','amount','is_approved','paymentMethod',"order","invoice"]

i try to get all orders that didn't have payments

Adnan
  • 23
  • 4

3 Answers3

0

Try this:-

Order.objects.exclude(id__in=Payment.objects.all().values_list('order', flat=True))

You should get all the orders which doesn't have payments linked.

Manish Shah
  • 331
  • 5
  • 18
0
You can filter this with:
Order.objects.filter(payment=None)
This thus filters all Order objects for which the payment ForeignKey relation is not set (is NULL).
You can then serialize this with:
OrderSerializer(Order.objects.filter(payment=None), many=True)
Note: It is normally better to use a OneToOneField [Django-doc] here, since an Order should have at most one Payment. In case of a ForeignKey, an Order can have multiple Payment objects related to it.
0
The payment field is defined as a ForeignKey, and by default Django appends _set to the name of the relation. So here you should use:
Order.objects.filter(payment_set=None)
This thus filters Orders for which the payment_set is empty, which implies no Payment object relates to the Order.
Django forms this "reverse relation" name by appending _set, since a ForeignKey typically defines a one-to-many relation, and "_set" is a common naming convention for collections.
You can override this default related_name with:
payment = models.ForeignKey(
    Payment,
    on_delete=models.CASCADE,
    related_name='orders'
)
Then you can filter with:
Order.objects.filter(orders=None)