I’m facing an issue with filtering CartOrderItems
by Vendor
using Django’s ORM. Here’s the scenario and the problem I’m encountering:
1.Scenario:
I have a Django application where vendors can upload products (Product
model) and manage their orders (CartOrderItems
and CartOrder
models).
2.Objective:
I want to fetch orders (CartOrder
instances) associated with products uploaded by the current vendor (request.user
).
3Current Approach:
In my view function vendor_pannel1
, I’m trying to achieve this as follows:
@login_required
def vendor_pannel1(request):
# Get products uploaded by the current vendor (user)
vendor_products = Product.objects.filter(user=request.user)
# Get the vendor IDs associated with these products
vendor_ids = vendor_products.values_list("vendor_id", flat=True)
# Get CartOrderItems related to those vendors
order_items = CartOrderItems.objects.filter(vendor_id__in=vendor_ids)
# Get orders related to those CartOrderItems
orders = CartOrder.objects.filter(cartorderitems__in=order_items).distinct()
context = {
"orders": orders,
}
return render(request, "vendorpannel/dashboard.html", context)
- Issue:
It is filtering nothing.
5.Expected Outcome:
I expect to retrieve orders (CartOrder
instances) related to products uploaded by the current vendor (request.user
).
6.My Models:
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)default
order=models.ForeignKey(CartOrder,on_delete=models.CASCADE)
# product = models.ForeignKey(Product, on_delete=models.CASCADE, default=1) #please dont't ignore it
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))
7.What I’ve Tried:
I’ve tried altering the filter conditions (vendor__in=vendor_products
) and exploring other Django ORM methods, but haven’t been successful in resolving the issue.
Request:
Could someone please guide me on how to correctly filter CartOrderItems by Vendor using Django ORM in the context of my application structure?