Search code examples
pythonjsondjangodjango-rest-frameworkmany-to-many

Django ManyToMany get error 'Object of type QuerySet is not JSON serializable'


i have this issue on my application. Object of type QuerySet is not JSON serializable In fact, i creating an application in which we have a relationship of the type several Products inone or more Orders.

Here is my code :

models.py

# 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)
    supply = models.ForeignKey(Supplies, on_delete = models.CASCADE)
    statut = models.CharField(max_length=500, default="validé")
    date_of_order = models.DateField()
    products = models.ManyToManyField(Products, through='OrderProduct')
    class Meta:
        db_table ="orders"


# model OrderProduct
class OrderProduct(models.Model):
    Order = models.ForeignKey(Orders, on_delete=models.CASCADE)
    product = models.ForeignKey(Products, on_delete=models.CASCADE)
    product_quantity = models.PositiveIntegerField(default=1)
    class Meta:
        db_table ="ordersproducts"

views.py

# OrderGetAllApi
@csrf_exempt
def order_list(request):
    orders = Orders.objects.select_related('supply').all
    # orders = Orders.objects.select_related('products').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,
            'supply_id': order.supply.supply_id,
            'supply_name': order.supply.supply_name,
            'supply_address': order.supply.supply_address,
            'products': order.products.all()
        })
    return JsonResponse(order_list, safe=False)

serializers.py

class OrderProductSerializer(serializers.ModelSerializer):
    product = ProductSerializer()

    class Meta:
        model = OrderProduct
        fields = ['product_id', 'product_name', 'product_quantity']
        # fields = '_all_'


class OrderSerializer(serializers.ModelSerializer):
    supply = SupplySerializer()
    products = OrderProductSerializer(many=True)

    class Meta:
        model=Orders
        fields=('order_id', 'order_number', 'date_of_order', 'statut', 'supply', 'products')

I want to get the list of order with their respective products. when i enter the list od orders (order_list) in the browser And i get the error Object of type QuerySet is not JSON serializable. How can fix the problem and get the list of Orders with their products.

Thank You


Solution

  • Here is the good code:

    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,
        'supply_id': order.supply.supply_id,
        'supply_name': order.supply.supply_name,
        'supply_address': order.supply.supply_address,
        'products': list(order.products.values()) 
      })
    return JsonResponse(order_list, safe=False)
    

    )