Skip to content

Confusion between Cart and CartItem #1

@SanskarSans

Description

@SanskarSans

I got confuse with the model carts because there are two tables used Cart and CartItem. Can you tell me their purposes with an example, please? Because if the model was something like this then i understand

class Cart(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

class CartItem(models.Model):
    cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField(default=1)
    # line_item_total also i could not understand.
    line_item_total = models.DecimalField(max_digits=10, decimal_places=2)

But you have used items twice both in Cart and Cart Items. In cart as M2M field and in CartItem as FK.

Sorry i could not understand. Can you please explain this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions