1
This is just my opinion…
An Item
should probably have a quantity
field used to keep track of how many are in inventory at a given time. This field gets updated every time an order is placed.
Your “Order” form class needs to validate that for every item in the order, there are enough of that item left, or throw a validation error, or create a back order, etc.
ItemInventory
should probably change to something like “LineItem” – it represents how many of which item was ordered. It should also probably have a “price” field to record what the price was at the time the order was placed. This model needs a foreign key to Order
.
If your LineItem doesn’t also include the price, and you change the price of the related Item, every single order total could potentially be incorrect if you were just relying on Item.price
Order
doesn’t need an “items” field as you can use the order.lineitem_set.all()
queryset to retrieve these items. You could also use a Sum of the price column on that queryset to generate the total instead of making it a column, but that’s subjective.
0
I do a lot of database work for a POS software company, and this is the structure of most of their databases.
– ItemMaster (stores data about the item eg. inventory, price, etc)
– TransactionMaster (stores master data about the transaction (sum, tender, balance, etc)
– TransactionDetail (provides a line for each item in a transaction with TransactionMaster as a foreign key reference)
This method allows you to grab Transactions, then grab the details by the foreign key reference, and the details table will hold the price, quantity, etc for each line item on the invoice. I’ve seen this method in multiple software, seems to be pretty reliable.
- [Answer]-Python. Django1.7 DoesNotExist. Matching query does not exist
- [Answer]-Working with links in Django
- [Answer]-Django: Serialize to JSON a dictionary with items that can include querysets
- [Answer]-How to update standart Django model (auth_user)
- [Answer]-Assign an Instance in a ForeingKey in Django