Cannot query "CartOrder object (21)": Must be "User" instance.

I’m fairly new to Django / Channels so apologies if everything required isn’t being shown here. I’d be happy to provide further info if necessary.
Anyway, I’ve been working on an Ecommerce Website and am running into this error:

ValueError at /checkout/53469/
Cannot query "CartOrder object (21)": Must be "User" instance.

Here my checkout View:

@login_required
def checkout(request, oid):
    order = CartOrder.objects.get(oid=oid)
    order_items = CartOrderItems.objects.filter(order=order)


    context = {
        "order":order,
        "order_items":order_items,
    }

    return render(request, "core/checkout.html", context)

Checkout URL

    path('checkout/<oid>/', checkout, name="checkout"),

Checkout Model:

class CartOrder(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    full_name = models.CharField(max_length=100, null=True, blank=True) 
    email = models.CharField(max_length=100, null=True, blank=True) 
    phone = models.CharField(max_length=100, null=True, blank=True) 

    address = models.CharField(max_length=100, null=True, blank=True)
    city = models.CharField(max_length=100, null=True, blank=True)
    state = models.CharField(max_length=100, null=True, blank=True)
    country = models.CharField(max_length=100, null=True, blank=True)
    
    price = models.DecimalField(max_digits=12, decimal_places=2, default="0.00")
    saved = models.DecimalField(max_digits=12, decimal_places=2, default="0.00")
    
    shipping_method = models.CharField(max_length=100, null=True, blank=True) 
    traking_id = models.CharField(max_length=100, null=True, blank=True) 
    traking_website_address = models.CharField(max_length=100, null=True, blank=True) 

    paid_status = models.BooleanField(default=False, null=True, blank=True)
    order_date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
    product_status = models.CharField(choices=STATUS_CHOICE, max_length=30, default="processing") 
    sku = ShortUUIDField(null=True, blank=True, length=5, prefix="SKU", max_length=20, alphabet="1234567890")
    oid = ShortUUIDField(null=True, blank=True, length=5, max_length=20, alphabet="1234567890")

    stripe_payment_intent = models.CharField(max_length=1000, null=True, blank=True) 

    #This class is for showing changed class name in Dashboard 
    class Meta:
        verbose_name_plural = "Cart Order"


Checkout FronEnd:

{% for o in order_items %}
                            <div class="clo-lg-6 mb-2">
                                <div class="card" style="max-width:540px">
                                    <div class="row g-0">
                                        <div class="col-sm-4">
                                            <img src="{{o.image}}" style="width:100%; height: 100%; object-fit:cover;" class="rounded-start" alt="Card image" />
                                        </div>
                                        <div class="col-sm-8">
                                            <div class="card-body">
                                                <h5 class="card-title">{{o.item}}</h5>
                                                <p class="card-text fs-sm">Qty: {{o.qty}}</p>
                                                <p class="card-text fs-sm">Price: ${{o.price}}</p>
                                                <p class="card-text fs-sm">Total: ${{o.total}}</p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        {% endfor %}

It looks like the problem is on the CartOrdemItems model, because the error is pointing to a CartOrder instance being passed to an attribute that’s expecting another type of object. And on your view this is happening here:

So, in this case you’re using the order variable that is an instance of CartOrder to filter/query the CartOrderItems model, using the order field defined there.
I would double check if the order field on the CartOrderItems model is pointing to the correct model: CartOrder and not User that’s what seems to be set currently.

CardOrderItems Model:

class CartOrderItems(models.Model):
    order = models.ForeignKey(User, on_delete=models.CASCADE)  
    invoice_no = models.CharField(max_length=200)  
    product_status = models.CharField(max_length=200)  
    item = models.CharField(max_length=200)  
    image = models.CharField(max_length=200)  
    qty = models.IntegerField(default=0)  
    price = models.DecimalField(max_digits=12, decimal_places=2, default="2.50")
    total = models.DecimalField(max_digits=12, decimal_places=2, default="2.50")

    #This class is for showing changed class name in Dashboard 
    class Meta:
        verbose_name_plural = "Cart Order Items"

    
    def order_img(self):
        return mark_safe('<img src="/media/%s" width="50" height="50" />' % (self.image))

Well, that’s it.
Shouldn’t the order ForeignKey field point to the CartOrder model?

Brother,
if i don’t use order ForeignKey field, then how can i access CartOrder Model through order variable?

order = CartOrder.objects.get(oid=oid)
order_items = CartOrderItems.objects.filter(order=order)

I don’t think you understood my statement.
I’m no saying to remove the foreign key field, i’m saying that it should point to other model.
Here, let me show you.

class CartOrderItems(models.Model):
    order = models.ForeignKey(
        User,  # <---- This shouldn't be `User` but `CartOrder` instead
        on_delete=models.CASCADE
    ) 
2 Likes

Thanks a lot brother. I understand your point. But when i replace the CartOrder instead of User, it shows another problem.

ValueError at /save_checkout_info/
Cannot assign "<SimpleLazyObject: <User: Afnan>>": "CartOrderItems.order" must be a "CartOrder" instance.

Well, that’s another issue, on another part of the code.

Is that mean i don’t have any field named order in CartOrder model?

the user attr in CartOrderItems is User object, but you send Order object to user attr…
that is problem.

a = User.objects.get({some})
b = CartOrderItems.objects.filter(order=a)

if you set wrong foreign key, change model.

order = models.ForeignKey(CartOrder, on_delete=models.CASCADE)