Home > Software engineering >  A Difficult Object Query
A Difficult Object Query

Time:01-03

So, I have an e-commerce website that I am building. When the customer clicks "place order", the site creates the order, and the database updates totally fine. The problem lies in displaying the order summary. IF the customer has ordered from the company in the past, that order is stored in the database along with the new one. The site then throws the error "2 objects returned" when I try to access it through the foreign key that relates the order table to the User table. The reason for this is obvious to me so I added a datetime field and set the auto_now_add attribute to TRUE. The problem I'm having is selecting the latest order for that particular customer. Is there a way to use something like this?

user = request.user
Order.objects.filter(Customer=user).date_placed.latest()

The fact that filter() returns a set makes me think that sytax won't execute properly. Maybe with a for loop inside a function that takes User as a parameter? I have no idea how to approach this one and thought I'd reach out. Thanks you guys.

View for the request:

def orderSummary(request):
    context = {}

    if request.user.is_authenticated:

        owner = request.user.id
        user = request.user
        cart = Cart.objects.get(cart_owner=owner)
        held_items = Order_holding.objects.filter(cart=cart)
        new_order = Order(customer=user)
        new_order.save()
        order = Order.objects.get(customer = user)

        for item in held_items:
            order_item = Order_item(order=order, blackAndWhite=item.blackAndWhite, product= item.product, color = item.color, gloss=item.gloss, matte=item.matte, size=item.size, notes=item.notes, cuts=item.cuts, grommets=item.grommets, quantity=item.quantity, subtotal=item.subtotal, cart=item.cart)
            order_item.save()
            item.delete()

        
        order_items = Order_item.objects.filter(cart=cart)

        for item in order_items:
            order.items.add(item)
            order.save()

        context['order_items'] = order.items.all()


        return render(request, 'order/orderSummary.html', context)
    else:
        return HttpResponseRedirect(reverse("login"))

The order.models.py file:

from django.db import models
from django.contrib.auth.models import User
from products.models import Product
from cart.models import Cart


class Order_item(models.Model):
    #front_image = models.ImageField()
    blackAndWhite = models.BooleanField(default=False)
    product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True)
    color = models.BooleanField(default=False)
    gloss = models.BooleanField(default=False)
    matte = models.BooleanField(default=False)
    size = models.CharField(null=True, blank=True, max_length=50)
    notes = models.CharField(null=True, blank=True, max_length=1000)
    cuts = models.IntegerField(null=True, blank=True, max_length=200)
    grommets = models.IntegerField(null=True, blank=True, max_length=200)
    quantity = models.IntegerField(null=True, blank=True, max_length=200)

    cart = models.ForeignKey(Cart, on_delete=models.CASCADE, null=True)

    subtotal = models.DecimalField(max_digits=100, decimal_places=2, default=0.00)


class Order(models.Model):
    customer = models.ForeignKey(User, on_delete=models.CASCADE)
    items = models.ManyToManyField(Order_item, null=True)
    time_placed = models.DateTimeField(auto_now_add=True, null=True)

    
class Order_holding(models.Model):
    blackAndWhite = models.BooleanField(default=False)
    product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True)
    color = models.BooleanField(default=False)
    gloss = models.BooleanField(default=False)
    matte = models.BooleanField(default=False)
    size = models.CharField(null=True, blank=True, max_length=50)
    notes = models.CharField(null=True, blank=True, max_length=1000)
    cuts = models.IntegerField(null=True, blank=True, max_length=200)
    grommets = models.IntegerField(null=True, blank=True, max_length=200)
    quantity = models.IntegerField(null=True, blank=True, max_length=200)

    cart = models.ForeignKey(Cart, on_delete=models.CASCADE, null=True)
    order = models.ForeignKey(Order, on_delete=models.CASCADE, null=True)

    subtotal = models.DecimalField(max_digits=100, decimal_places=2, default=0.00)

CodePudding user response:

Try :

Order.objects.filter(customer=user).order_by('-time_placed')

CodePudding user response:

I think you can do this:

Order.objects.filter(customer=user).latest("time_placed")

This returns only one object.

  • Related