Search code examples
djangomodelrelational-databaseinventory

django party rental company inventory model


A party company rents out chairs, table and tents. These all have a set price, but there are savings if the items are bungled in a package(e.g. Package A includes: one 18x30 tent, 72 chairs and 12 tables). I'm dealing with three different packages: a, b and c. My model so far is as follows.

from django.db import models
from django.contrib.auth.models import User


class Item(models.Model):
    name = models.CharField(max_length=255)
    price = models.DecimalField(decimal_places=2, max_digits=5)

class ItemInventory(models.Model):
    item = models.ForeignKey(Item)
    quantity = models.PositiveIntegerField()

class Order(models.Model):
    user = models.ForeignKey(User)
    items = models.ManyToManyField(Item)
    total = models.DecimalField(decimal_place=2, max_digits=5)

So, an Item could be a chair, table, 30x40 tent, etc., those items are linked to ItemInventory to count how many are left to rent out. An Order can include any Item of some arbitrary amount. Order.total would sum up to be each (Order.items.price x quantity).

Question 1, How might I include quantity for each Item in an Order?

Question 2, Should I make a model for packages or include package logic in views?


Solution

  • 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.