Bad Request: /api/inventory/stock/1/

Please check /app-frontend/src/Components/StockList.js

When I try to edit a stock, it returns this:
Bad Request: /api/inventory/stock/1/
[19/Jan/2025 00:14:44] “PUT {same link as above} HTTP/1.1” 400 42

This is as if the payload is not in the expected format required by django.

Django with Rest Framework

models.py

from django.db import models


class Category(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.name


class RawMaterial(models.Model):
    category = models.ForeignKey(Category, related_name='raw_materials', on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    width = models.FloatField()  # Width in meters
    length = models.FloatField()  # Length in meters
    remaining_quantity = models.FloatField()  # Remaining length in meters

    def __str__(self):
        return f"{self.name} ({self.remaining_quantity}m left)"


class Product(models.Model):
    category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
    code = models.CharField(unique=True, blank=True, max_length=20)
    size = models.IntegerField(default=0)
    description = models.TextField(blank=True, null=True)
    price = models.FloatField(default=0)
    quantity_in_stock = models.PositiveIntegerField(default=0)  # Total quantity in all warehouses

    def __str__(self):
        return f"{self.description} (Code: {self.code} Size: {self.size}cm {self.quantity_in_stock} pcs in stock)"

    def update_quantity_in_stock(self):
        total_quantity = Stock.objects.filter(product=self).aggregate(models.Sum('quantity'))['quantity__sum']
        self.quantity_in_stock = total_quantity if total_quantity is not None else 0
        self.save()


class Warehouse(models.Model):
    name = models.CharField(max_length=100)
    location = models.CharField(max_length=200)
    description = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.name


class Stock(models.Model):
    product = models.ForeignKey(Product, related_name='stock_entries', on_delete=models.CASCADE)
    warehouse = models.ForeignKey(Warehouse, related_name='stock', on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField()

    def __str__(self):
        return f"{self.product.description} - {self.quantity} units in {self.warehouse.name}"

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        self.product.update_quantity_in_stock()

Welcome @hemanthkramakrishnan !

Couple different things I’d like to point out here.

First, the Django REST Framework is a third-party package, with its own support channels. While there are some people here who do attempt to address DRF-related issues, you might get faster or quicker answers from one of those resources.

Next, When posting code here, enclose the code between lines of three
backtick - ` characters. This means you’ll have a line of ```, then your code,
then another line of ```. This forces the forum software to keep your code
properly formatted. (I have taken the liberty of correcting your original posts.
Please remember to do this in the future.)

Next, when you get an error, please make sure you post the complete error along with any traceback you might get. Do not try to summarize or anonymize that information. We generally need to see the complete and accurate information to make a diagnosis.

Finally, you are more likely to attract attention to this issue if you provide a bit of a road map for the code. People are less likely to read through an entire project if they don’t have any real idea of where to look.

So I suggest you identify the specific files, functions, and models involved. In this case, this would mean including information of what views, urls and other files handling this request.