Many to many field with extra fields in Django REST Framework
I have two models: Purchase and Product, in the Purchase model I have some fields and among them a many to many referencing the Product, but for each product inserted in the purchase I need to add a status field, price and quantity and I have no idea how I could do this. Later I need to change these fields and I also need to have the option of adding more products, changing a product or excluding it from the purchase.
Purchase
class Purchase(models.Model):
id = models.UUIDField(primary_key=True, unique=True, default=uuid4, editable=False)
created_at = models.DateTimeField(_("Created At"), default=timezone.now)
request_date = models.DateField(_("Request Date"), default=date.today)
obs = models.TextField(_("Observation"))
status = models.CharField(_("Status"), choices=STATUS, default="Pending", max_length=8)
products = models.ManyToManyField(Product, verbose_name=_("Products"))
approval_date = models.DateField(_("Approval Date"), blank=True, null=True)`
Product
class Product(models.Model):
id = models.UUIDField(primary_key=True, unique=True, default=uuid4, editable=False)
code = models.CharField(_("Code"), max_length=30)
un = models.CharField(_("Measurement Unit"), max_length=30)
description = models.TextField(_("Description"))`
I've tried to create a PurchaseProduct model but I couldn't make it work properly.
PurchaseProduct
class PurchaseProduct(models.Model):
purchase = models.ForeignKey(Purchase, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.DecimalField(_("Quantity"), max_digits=10, decimal_places=2)
price = models.DecimalField(_("Price"), max_digits=10, decimal_places=2)
status = models.CharField(_("Status"), choices=STATUS, default="Pending", max_length=8)`