Hi, I have a question about how to create a sort of ecommerce in Django.
If I have models for Product and Order (that contains products in a manytomanyfield), I have to store the items in the order with an “Item” class (that keep the quantity for example). But this will create an object “Item” for every item in an order, with a lot of repeated data in the DB.
Is there a better way to do this?
Actually, it’s not “repeated data”. An Item in an Order is a representation of the quantity of a Product in an Order. Even though two people might each be buying 2 of a Product, it’s not (from a database-analysis perspective) duplicate data.
This is appropriately modelled either as additional data on the ManyToMany join table, or (less-frequently) on a table related to that join table.
So you may have a Product table containing the items to be purchased, a Buyer who is making a purchase, an Order as a join table between Buyer and Product, and Item, which are the individual items (perhaps with quantity), having a ForeignKey to Order.
This is all a fairly standard design for this type of “invoicing” - whether it’s a purchase order or something else.