Question: I have created a very small Django application to manage a very small Inventory. My models.py code is: class Inventory(models.Model): account = models.ForeignKey( "accounts.Account", on_delete=models.DO_NOTHING, null=False, blank=False ) class InventoryProduct(models.Model): inventory = models.ForeignKey("Inventory", on_delete=models.CASCADE) sku = models.ForeignKey( "products.SKU", on_delete=models.DO_NOTHING, null=False, blank=False ) quantity = models.PositiveIntegerField( default=0 ) class Transaction(models.Model): IN = 1 OUT = 0 TYPE_CHOICES = ( (IN, "Incoming"), # Carico / Entrata (OUT, "Outgoing"), # Scarico / Uscita ) inventory = models.ForeignKey("Inventory", on_delete=models.CASCADE) transferred_to = models.ForeignKey( "Inventory", on_delete=models.CASCADE, blank=True, null=True ) code = models.UUIDField(default=uuid.uuid4) transaction_type = models.PositiveSmallIntegerField( choices=TYPE_CHOICES, default=IN ) transaction_date = models.DateTimeField(auto_now_add=True) notes = models.TextField(null=True, blank=True) class TransactionItem(models.Model): transaction = models.ForeignKey("Transaction", on_delete=models.CASCADE) item = models.ForeignKey("InventoryProduct", on_delete=models.CASCADE) quantity = models.IntegerField() def save(self, *args, **kwargs): super().save(*args, **kwargs) self.item.quantity += self.quantity self.item.save()  The code is quite explanatory, I basically have an Inventory per account and the Inventory has products that I add in the related model InventoryItem. Each product in InventoryItem has a quantity that will be updated during a Transaction. A Transaction has a type IN/OUT to understand if i have to add or remove Items from the Inventory. Lastly, as you can surelly understand the TransactionItem has all the Items with the quantity (to add or remove) inside a Transaction. It is quite simply, I really appreciate your opinion if i can improve the modelling somehow. My questions are:  Add a way to transfer products from an Inventory to another (i think i need an OUT Transaction and then an IN Transaction), but how can I keep track of this "movement" from an Inventory to another? I would like to save from what Inventory the products are coming from.  This question is also related to the first one. Products can be added to an Inventory from an Order or by a transfer from another Inventory. How can I also include the Order "concept"?  

Answer: 1. Tracking Transfers Between Inventories To keep track of where the products are coming from, you might consider adding a transferred_from field as well. This will allow you to create a direct link between the source and destination inventories for each transfer. class Transaction(models.Model): # Existing fields... transferred_from = models.ForeignKey( "Inventory", related_name="transferred_from_transactions", on_delete=models.CASCADE, blank=True, null=True ) transferred_to = models.ForeignKey( "Inventory", related_name="transferred_to_transactions", on_delete=models.CASCADE, blank=True, null=True )  Example Query: # Assuming 'last_transaction' is a Transaction instance last_transaction = Transaction.objects.last() # To find out where the product came from if last_transaction.transferred_from: source_inventory = last_transaction.transferred_from print(f"Product came from Inventory ID: {source_inventory.id}") else: print("This product did not come from another inventory.")  2. Including the Order Concept Add new model: Order and OrderItem Models class Order(models.Model): order_code = models.UUIDField(default=uuid.uuid4, editable=False) order_date = models.DateTimeField(auto_now_add=True) # Additional fields as needed class OrderItem(models.Model): order = models.ForeignKey(Order, on_delete=models.CASCADE) sku = models.ForeignKey("products.SKU", on_delete=models.CASCADE) quantity = models.PositiveIntegerField(default=0)  Next, add a reference to an Order in your Transaction model: class Transaction(models.Model): # Existing fields... order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True, blank=True, related_name="transactions")  Example Usage: # Assuming 'some_transaction' is a Transaction instance some_transaction = Transaction.objects.select_related('order', 'transferred_from').last() # Check if the transaction is linked to an order if some_transaction.order: print(f"Transaction linked to Order ID: {some_transaction.order.id}") # Check where the products came from if some_transaction.transferred_from: print(f"Products transferred from Inventory ID: {some_transaction.transferred_from.id}") else: print("Products were not transferred from another inventory.")  Hope this helps.