Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I am creating some simple queries using the Django framework. I have one app which it has 8 models in models.py. Here are my classes:
from django.db import models


# Create your models here.
class Category(models.Model):
    category_id = models.AutoField(primary_key=True)
    category_name = models.CharField(max_length=15)
    description = models.TextField(null=True, blank=True)


    def __str__(self):
        return f"ID: {self.category_id}, {self.category_name}"


class User(models.Model):
    user_id = models.AutoField(primary_key=True)
    first_name = models.CharField(max_length=10)
    last_name = models.CharField(max_length=20)
    mobile_number = models.CharField(max_length=11, unique=True)
    birth_date = models.DateField()
    country = models.CharField(max_length=15)
    city = models.CharField(max_length=15)
    region = models.CharField(max_length=15)
    address = models.TextField(null=True, blank=True)
    postal_code = models.CharField(max_length=10, unique=True)
    is_active = models.BooleanField(default=True)

    def __str__(self):
        return f"{self.first_name} {self.last_name}"


class Company(models.Model):
    company_id = models.AutoField(primary_key=True)
    company_name = models.CharField(max_length=30)

    def __str__(self):
        return f"ID: {self.company_id}, {self.company_name}"


class Customer(models.Model):
    customer_id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    company = models.ForeignKey(Company, on_delete=models.CASCADE)
    contact_name = models.CharField(max_length=30, unique=True)
    contact_title = models.CharField(max_length=30, null=True)

    def __str__(self):
        return f"User {self.user.__str__()}  has ordered a ### from {self.company.company_name} company.\n"


class Employee(models.Model):
    employee_id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=30, null=True, blank=True)
    hire_date = models.DateField(null=True, blank=True)

    def __str__(self):
        return f"ID: {self.employee_id}, {self.user.__str__()} {self.title} {self.user.address}"


class Product(models.Model):
    product_id = models.AutoField(primary_key=True)
    product_name = models.CharField(max_length=40)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    unit_price = models.FloatField()

    def __str__(self):
        return f"ID: {self.product_id}, {self.product_name}: {self.unit_price}"


class Order(models.Model):
    order_id = models.AutoField(primary_key=True)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    order_time = models.DateTimeField(auto_now_add=True , auto_now=False)

    FREIGHT_CHOICES = (
        ('TR', 'Train'),
        ('AR', 'Airplane'),
        ('CA', 'Car'),
    )
    freight = models.CharField(choices=FREIGHT_CHOICES, max_length=10)

    def __str__(self):
        return f"ID: {self.order_id}, Ordered by: {self.customer.user.__str__()}"


class OrderDetail(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    unit_price = models.FloatField()
    quantity = models.SmallIntegerField(default=1)

    def __str__(self):
        return f"{self.order.customer.user.__str__()}/{self.product.product_name}:{self.unit_price * self.quantity}"


And I have a function in views.py :
def each_customer_ordered_products(request):
    
    customer_ordered_products =  OrderDetail.objects.filter(order__customer__user__first_name = 'Aylin')
    return HttpResponse(customer_ordered_products)


Which shows only the products which have been ordered by Aylin. Now I want to create a new variable in this method like a dictionary or a json object
that can save every customer's first name as key and the ordered products as value and after running the application I want to see the whole of the dictionary not just for one of the customers.

What I have tried:

Unfortunately, I do not have any idea and I do not know how to map the orders to the customers and then create a variable and then pass it to HttpResponse or JsonResponse.I will be grateful for your help or advice.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900