管理小型库存的 Django 应用程序

问题描述 投票:0回答:1

我创建了一个非常小的 Django 应用程序来管理一个非常小的库存。

我的models.py代码是:

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()

代码非常具有解释性,我基本上每个帐户都有一个 Inventory,并且 Inventory 具有我在相关模型 InventoryItem 中添加的产品。 InventoryItem 中的每个产品都有一个数量,该数量将在 Transaction 期间更新。

交易有一个输入/输出类型来了解我是否必须从库存中添加或删除项目。最后,您肯定可以理解,TransactionItem 包含交易内具有数量(要添加或删除)的所有项目。

很简单,如果我能以某种方式改进建模,我真的很感激你的意见。

我的问题是:

  1. 添加一种将产品从库存转移到另一个库存的方法(我想我需要一个 OUT 事务,然后是一个 IN 事务),但是我如何跟踪从一个库存到另一个库存的这种“移动”?我想从产品的库存中保存。

  2. 这个问题也和第一个问题有关。产品可以从订单或通过从另一个库存转移来添加到库存中。我怎样才能包含订单“概念”?

python django django-models
1个回答
0
投票

1。跟踪库存之间的转移

要跟踪产品的来源,您也可以考虑添加 transferred_from 字段。 这将允许您在每次传输的源库存和目标库存之间创建直接链接。

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
    )

查询示例:

# 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。包括订单概念

添加新模型:Order 和 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)

接下来,在您的交易模型中添加对订单的引用:

class Transaction(models.Model):
    # Existing fields...
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True, blank=True, related_name="transactions")

用法示例:

# 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.")

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.