Django : Get the list of a orderdetail

44 Views Asked by At

Hi i want to get the list (in json) first of the list of a Orderdetail. Indeed, I am trying to display all the order data with the details as mentioned below in the json code.

Here is my code :

models.py

# model Driver
class Drivers(models.Model):
    driver_id = models.AutoField(primary_key=True)
    driver_fullname = models.CharField(max_length=500)
    class Meta:
        db_table ="drivers"

# model Truck
class Trucks(models.Model):
    truck_id = models.AutoField(primary_key=True)
    truck_matricule = models.CharField(max_length=500)
    class Meta:
        db_table ="trucks"

# model Product
class Products(models.Model):
    product_id = models.AutoField(primary_key=True)
    product_name = models.CharField(max_length=500)
    class Meta:
        db_table ="products"


# model Order
class Orders(models.Model):
    order_id = models.AutoField(primary_key=True)
    order_number = models.CharField(max_length=500)
    statut = models.CharField(max_length=500, default="validé")
    date_of_order = models.DateField()
    company = models.ForeignKey(Companies, on_delete = models.CASCADE)
    products = models.ManyToManyField(Products, through='OrdersDetail')
    class Meta:
        db_table ="orders"


# model OrdersDetail
class OrdersDetail(models.Model):
    Order = models.ForeignKey(Orders, on_delete=models.CASCADE)
    product = models.ForeignKey(Products, on_delete=models.CASCADE)
    driver = models.ForeignKey(Drivers, on_delete = models.CASCADE)
    truck = models.ForeignKey(Trucks, on_delete = models.CASCADE)
    product_quantity = models.PositiveIntegerField(default=1)
    # amount = models.IntegerField(default=1)
    class Meta:
        db_table ="ordersdetails"

serializers.py

class TruckSerializer(serializers.ModelSerializer):
    class Meta:
        model=Trucks
        fields = '_all_'


class MeasureSerializer(serializers.ModelSerializer):
    class Meta:
        model=Measures
        fields = '_all_'


class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model=Products
        fields = '_all_'


class OrderDetailSerializer(serializers.ModelSerializer):
    company = CompanySerializer()
    products = ProductSerializer()
    truck = TruckSerializer()
    driver = DriverSerializer()

    class Meta:
        model = OrdersDetail
        fields = '_all_'


class OrderSerializer(serializers.ModelSerializer):
    products = OrderDetailSerializer(many=True)

    class Meta:
        model=Orders
        fields = '_all_'

views.py

 OrderGetAllApi
@csrf_exempt
def order_list(request):
    orders = Orders.objects.select_related('company').all()
#
    order_list = []
    for order in orders:
        order_list.append({
            'order_id': order.order_id,
            'order_number': order.order_number,
            'date_of_order': order.date_of_order,
            'company_id': order.company.company_id,
            'company_name': order.company.company_name,
            'company_email': order.company.company_email,
            'products' : list(order.products.values()),
            # i want the get here les list of all elements that appear in OrdersDetail related to their Order
        })
    return JsonResponse(order_list, safe=False)

here is the type of json list that I would like to obtain:

Sample expected output json code

    {
        "order_id": 1,
        "order_number": "RTE-05092301",
        "date_of_order": "2023-09-05",
        "company_id": 1,
        "company_name": "RT-ENTERPRISES",
        "products": [
            {
                "product_id": 2,
                "product_name": "Product 2"
            },
            {
                "product_id": 4,
                "product_name": "Product 4"
            },
            {
                "product_id": 3,
                "product_name": "Product 3"
            }
        ],
        # This part i want to fectch, how can get it please.
        "order_detail" : {
            "products": [
                {
                    "product_id": 2,
                    "product_name": "Product 2"
                },
                {
                    "product_id": 4,
                    "product_name": "Product 4"
                },
                {
                    "product_id": 3,
                    "product_name": "Product 3"
                }
            ],
            "truck": {
                "truck_id": 1
                "truck_matricule": "TRK001"
            },
            "drivers": [
                {
                    "driver_id": 3
                    "driver_fullname": "John Doe"
                }
            ],
            "product_quantity": 40
        }
    },

This is a example (order_detail) of what i want get from my code

0

There are 0 best solutions below