私はdjangoの新機能です。私は単純な在庫アプリケーションを作っています。私は何をしたいことはある在庫モデルを作成する最良の方法は何ですか?
class Received(models.Model):
item = models.ForeignKey(Item)
weight = models.DecimalField(max_digits=8, decimal_places=2)
....
class Sold(models.Model):
item = models.ForeignKey(Item)
weight = models.DecimalField(max_digits=8, decimal_places=2)
....
class Inventory(models.Model):
item = models.OneToOne(Item)
weight_received = ?
weight_sold = ?
timestamp = models.DateTimeField()
....
class InventoryHistory(models.Model):
# I have no idea
item = models.ForeignKey(Item)
date = models.DateField(auto_now=True)
total_weight = models.DecimalField(max_digits=8, decimal_places=2)
とき:
1)私はReceived
またはSold
上の入力データを行うInventory
が自動的Inventory.weight_in
がReceived.weight
とInventory.weight_out
のSUMがどこにあるか(を更新する必要がありますここに私のモデルです)Sold.weight
のSUMです。
2.)私はそれらを削除しますInventory
はが自動的に更新する必要があります
3)私は彼らに編集途中行い、Inventory
はが自動的
ことが可能であり、どのように更新すべきですか?
また、私のデータベース知識の問題に関するもう1つの質問があります。毎日在庫の履歴を追跡できるInventoryHistory
を作成する必要がありますか?
ありがとう...
ありがとう、それはうまくいきます。しかし、 'post_edit'はどうですか?そして、今のところ、データが入力されたときに 'Inventory.weight_in'と' Inventory.weight_out'にインクリメントを使います。私はあなたに尋ねたい、それは良いですか?または、私は(forループを使って)再カウントを行い、 'Inventory.weight_in'と' Inventory.weight_out'の値を置き換える必要がありますか? -ありがとうございます。 –
'post_save'は' created'パラメータを持ち、その値が 'True'の場合は新しいレコードが作成され、それ以外の場合はレコードが編集されました。 –
'weight_received'の値を計算するための集計を改善しました:' weight_received = Received.objects.aggregate(Sum( 'weight')) ' –