Sobes.tech
Junior — Senior

API endpoint for product purchase registration

livecode

Task condition

It is necessary to implement a view in Django REST Framework that will record the fact of a user purchasing a product. The verified product and user identifiers (for example, prod_id and usr_id) are already received as input. The task is to create a record in the Purchase model reflecting that this user has bought the specified product.

class User(db.Models):
    name = models.CharField(
        max_length=256,
        blank=True,
        default="",
        verbose_name="Name"
    )

class Product(db.Models):
    name = models.CharField(
        max_length=256,
        blank=True,
        default="",
        verbose_name="Name"
    )
    price = models.DecimalField()

class Purchase(db.Models):
    user = models.ForeignKey(
        model=User,
        related_name="purchase",
        on_delete=models.CASCADE
    )
    product = models.ForeignKey(
        model=Product,
        related_name="purchase",
        on_delete=models.CASCADE
    )
    count_products = models.IntegerField(default=1)