I’m working on a Django project where I have a vendor dashboard that should display all orders for the products uploaded by the current vendor. However, the dashboard is only showing orders placed by one user and not orders placed by other users for the same products.
Models:
Here are the relevant models:
class Product(models.Model):
pid=ShortUUIDField(length=10,max_length=100,prefix="prd",alphabet="abcdef")
user=models.ForeignKey(CustomUser, on_delete=models.SET_NULL ,null=True)
cagtegory=models.ForeignKey(Category, on_delete=models.SET_NULL ,null=True,related_name="category")
vendor=models.ForeignKey(Vendor, on_delete=models.SET_NULL,null=True,related_name="product")
color=models.ManyToManyField(Color,blank=True)
size=models.ManyToManyField(Size,blank=True)
title=models.CharField(max_length=100,default="Apple")
image=models.ImageField(upload_to=user_directory_path,default="product.jpg")
description=RichTextUploadingField(null=True, blank=True,default="This is a product")
price = models.DecimalField(max_digits=10, decimal_places=2, default=1.99)
old_price = models.DecimalField(max_digits=10, decimal_places=2, default=2.99)
specifications=RichTextUploadingField(null=True, blank=True)
tags=TaggableManager(blank=True)
product_status=models.CharField(choices=STATUS, max_length=10,default="In_review")
status=models.BooleanField(default=True)
in_stock=models.BooleanField(default=True)
featured=models.BooleanField(default=False)
digital=models.BooleanField(default=False)
sku=ShortUUIDField(length=10,max_length=100,prefix="sku",alphabet="abcdef")
date=models.DateTimeField(auto_now_add=True)
updated=models.DateTimeField(null=True,blank=True)
class Meta:
verbose_name_plural="Products"
def product_image(self):
return mark_safe('<img src="%s" width="50" height="50"/>'%(self.image.url))
def __str__(self):
return self.title
def get_percentage(self):
new_price=((self.old_price-self.price)/self.old_price)*100
return new_price
def get_product_price_by_size(self , Size):
return self.price + Size.objects.get(name = Size).price
class CartOrder(models.Model):
user=models.ForeignKey(CustomUser,on_delete=models.CASCADE)
item=models.CharField(max_length=100)
price= models.DecimalField(max_digits=10, decimal_places=2,default="1.99")
paid_status=models.BooleanField(default=False)
order_date=models.DateTimeField(auto_now_add=True)
product_status=models.CharField(choices=STATUS_CHOICE, max_length=30,default="processing")
class Meta:
verbose_name_plural="Cart Order"
class CartOrderItems(models.Model):
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
vendor = models.ForeignKey(Vendor,on_delete=models.CASCADE,default=1) #I have to fix the default
order=models.ForeignKey(CartOrder,on_delete=models.CASCADE,related_name="cartorderitems")
# product = models.ForeignKey(Product, on_delete=models.CASCADE, default=1)
invoice_num = models.BigIntegerField(blank=True,null=True)
product_status=models.CharField(max_length=200)
item=models.CharField(max_length=100)
image=models.CharField(max_length=100)
qty=models.BigIntegerField(default=0)
price= models.DecimalField(max_digits=12, decimal_places=2,default="15")
total= models.DecimalField(max_digits=12, decimal_places=2,default="20")
color=models.ForeignKey(Color,on_delete=models.SET_NULL,null=True,blank=True)
size=models.ForeignKey(Size,on_delete=models.SET_NULL,null=True,blank=True)
class Meta:
verbose_name_plural="Cart Order Items"
def catagory_image(self):
return mark_safe('<img src="%s" width="50" height="50"/>'%(self.image.url))
def oder_img(self):
return mark_safe('<img src="/media/%s" width="50" height="50"/>'%(self.image))
View:
Here is the view that is supposed to fetch and display the orders:
@login_required
def vendor_pannel1(request):
# Get products uploaded by the current vendor (user)
vendor_products = Product.objects.filter(user=request.user)
print("Vendor Products:", vendor_products)
# Get CartOrderItems related to those products
order_items = CartOrderItems.objects.filter(invoice_num__in=vendor_products)
# print("Order Items:", order_items)
# Get orders related to those CartOrderItems
orders = CartOrder.objects.filter(cartorderitems__in=order_items).distinct()
print("Orders:", orders)
context = {
"orders": orders,
}
return render(request, "vendorpannel/dashboard.html", context)
Issue:
When I place an order as one user, it shows up in the dashboard. However, when another user places an order for the same product, it does not appear in the dashboard.
What I’ve Tried:
Verified that the vendor_products
queryset is returning the correct products.
Verified that the order_items
queryset contains the correct order items.
Used .distinct()
to ensure no duplicate orders.
Expected Behavior:
I expect the dashboard to display all orders related to the products uploaded by the current vendor, regardless of which user placed the order.
Actual Behavior:
The dashboard only shows orders from one user and does not display orders from other users for the same products.
Any insights or suggestions on why this might be happening and how to resolve it would be greatly appreciated!