<filename>django_ecommerce/orders/models.py
from django.db import models
from django.conf import settings

from products.models import Product


class Customer(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        blank=True,
        null=True,
    )
    name = models.CharField(max_length=200, null=True)

    def __str__(self):
        return self.name


class Order(models.Model):
    customer = models.ForeignKey(
        Customer,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
    )
    date_ordered = models.DateTimeField(auto_now_add=True)
    complete = models.BooleanField(default=False)
    transaction_id = models.CharField(max_length=100, null=True)

    def __str__(self):
        return str(self.id)

    @property
    def get_basket_price(self):
        order_items = self.orderitem_set.all()
        cart_total = sum([item.get_total for item in order_items])

        return cart_total

    @property
    def get_basket_quantity(self):
        order_items = self.orderitem_set.all()
        cart_total = sum([item.quantity for item in order_items])

        return cart_total


class OrderItem(models.Model):
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
    quantity = models.IntegerField(default=0, null=True, blank=True)
    date_added = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = "Order Item"
        verbose_name_plural = "Order Items"

    def __str__(self):
        return f"{self.product.title} | ${self.product.price * self.quantity}"

    @property
    def get_total(self):
        total = self.product.price * self.quantity

        return total

<filename>django_ecommerce/orders/models.py
from django.db import models
from django.conf import settings

from products.models import Product


class Customer(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        blank=True,
        null=True,
    )
    name = models.CharField(max_length=200, null=True)

    def __str__(self):
        return self.name


class Order(models.Model):
    customer = models.ForeignKey(
        Customer,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
    )
    date_ordered = models.DateTimeField(auto_now_add=True)
    complete = models.BooleanField(default=False)
    transaction_id = models.CharField(max_length=100, null=True)

    def __str__(self):
        return str(self.id)

    @property
    def get_basket_price(self):
        order_items = self.orderitem_set.all()
        cart_total = sum([item.get_total for item in order_items])

        return cart_total

    @property
    def get_basket_quantity(self):
        order_items = self.orderitem_set.all()
        cart_total = sum([item.quantity for item in order_items])

        return cart_total


class OrderItem(models.Model):
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
    quantity = models.IntegerField(default=0, null=True, blank=True)
    date_added = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = "Order Item"
        verbose_name_plural = "Order Items"

    def __str__(self):
        return f"{self.product.title} | ${self.product.price * self.quantity}"

    @property
    def get_total(self):
        total = self.product.price * self.quantity

        return total
