[Answered ]-Django ManyToManyField as multiset

2👍

Instead of storing Item‘s in your inventory, you can store InventoryItem‘s

class InventoryItem(models.Model):
    item = models.ForeignKey(Items)
    quantity = models.SmallIntegerField(default=1)
    inventory = models.ForeignKey(Inventory)

The idea is you introduce a middle class that handles the quantity for you, as well as potentially other things like should the item be stackable.

The only alternative I can think of is to duplicate an item object and then store both of these in the inventory – which may or may not be better for your use case

👤Sayse

Leave a comment