Django: 'QuerySet' object has no attribute 'product_obj' when running for loop

I am trying to loop through a queryset, get the product object and remove the qty of items ordered from the product’s stock amount.

This is how my model Looks

class CartOrderItem(models.Model):
    order = models.ForeignKey(CartOrder, on_delete=models.CASCADE)
    product_obj = models.ForeignKey(Product, on_delete=models.CASCADE)
    qty = models.IntegerField(default=0)
    date = models.DateTimeField(auto_now_add=True, null=True, blank=True)

And this is how it looks in the admin section

This is how i ran the forloop

def PaymentSuccessView(request):
    order_items = CartOrderItem.objects.filter(order=order, order__payment_status="paid")
    for o in order_items.product_obj:
        print(o.title)  # print all the products title
        print(o.stock_qty)  # print the stock qty from the Product model
        # I want to substract the CartOrderItem's qty from the Product's stock qty

It then shows this error that says

'QuerySet' object has no attribute 'product_obj'

This error is saying that the queryset for the CartOrderItems models does not have an attribute "product_obj", but i clearly have a 'product_obj' field in my CartOrderItems

This is how i wanted to substract the items qty from the product stock qty, i am not sure this would work and i haven’t tried it because the error above would not allow me

for o in order_items.product_obj:
    o.product_obj.stock_qty -= o.qty

Hi desphixs :wave:
product_obj is attribute to CartOrderItems object, not to QuerySet objects.

you can write for loop like this:

for o in order_items:
1 Like