djangoorderswebshop

Internet shop in django. How to filter orders by user?


For my project i wrote account, cart, orders and shop apps.

order/models:

from django.db import models
from shop.models import Product


class Order(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.EmailField()
    address = models.CharField(max_length=250)
    postal_code = models.CharField(max_length=20)
    city = models.CharField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    paid = models.BooleanField(default=False)

    class Meta:
        ordering = ('-created',)

    def __str__(self):
        return 'Order {}'.format(self.id)

    def get_total_cost(self):
        return sum(item.get_cost() for item in self.items.all())


class OrderItem(models.Model):
    order = models.ForeignKey(Order, related_name='items',
                              on_delete=models.CASCADE)
    product = models.ForeignKey(Product, related_name='order_items',
                                on_delete=models.CASCADE)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    quantity = models.PositiveIntegerField(default=1)

    def __str__(self):
        return '{}'.format(self.id)

    def get_cost(self):
        return self.price * self.quantity

account/models is empty

account/views:

from django.contrib.auth import login as auth_login
from django.shortcuts import render, redirect
from .forms import SignUpForm
from orders.models import OrderItem, Order
from django.contrib.auth.decorators import login_required


def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            auth_login(request, user)
            return redirect('/')
    else:
        form = SignUpForm()
    return render(request, 'accounts/signup.html', {'form': form})


@login_required
def account(request):
    my_orders = Order.objects.all()
    order_items = OrderItem.objects.all()
    return render(request, "accounts/my_acc.html",
                  {"my_orders": my_orders, "order_items": order_items})

As you can see in account function i get all orders , but i don't know how to filter orders for user. What do i want is - when user enter in his account he can see all his previous orders.

How can i do that?


Solution

  • You first need to associate your Order with a User:

    from django.contrib.auth import get_user_model
    
    User = get_user_model()
    
    class Order(models.Model):
    
        user = models.ForeignKey(User, on_delete=models.PROTECT, related_name='orders')
        # insert other fields here
    

    See referencing the user model for what is going on here.

    Now you have a relationship between Order and User, which means you can filter for a given user's orders like this:

    def account(request):
        my_orders = Order.objects.filter(user=request.user)
    

    You will of course need to associate an Order with a User when the order is created in the first place - whatever code that creates the order will need to do that.