Tl dr: should I use an intermediate model “through” or a custom handler for adding a quantity to a many to many field
Context: I’m building a cashier system as a learning project. An order can contain different quantities of dishes. An order could have two club sandwiches for instance.
Google gemini suggested a through model coupling the sandwich to a quantity and adding this to the order.
An alternative is to use a custom manager and add methods for adding sandwiches.
The through model seems to generate overhead as this creates a dB table.
By definition, a many-to-many relationship is created within a relational database as a join-table (what Django refers to as the “through” table) between the two related tables. This is exactly how such things are modeled.
This is what gemini proposed.
This or a through model with a quantity field.
I suppose that would be easier to couple to a form later on now that I think of it.
def add_sandwich(self, order, sandwich, quantity):
# Logic to add sandwich to order with specified quantity
pass
def remove_sandwich(self, order, sandwich):
# Logic to remove sandwich from order
pass