2016-04-22 14 views
0

私は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_inReceived.weightInventory.weight_outのSUMがどこにあるか(を更新する必要がありますここに私のモデルです)Sold.weightのSUMです。

2.)私はそれらを削除しますInventoryが自動的に更新する必要があります

3)私は彼らに編集途中行い、Inventoryが自動的

ことが可能であり、どのように更新すべきですか?

また、私のデータベース知識の問題に関するもう1つの質問があります。毎日在庫の履歴を追跡できるInventoryHistoryを作成する必要がありますか?

ありがとう...

答えて

0

signalsを使用できます。特にdjango.db.models.signals.post_saveおよびdjango.db.models.signals.post_deleteである。追跡履歴については、django-reversionを使用することをお勧めします。

+0

ありがとう、それはうまくいきます。しかし、 'post_edit'はどうですか?そして、今のところ、データが入力されたときに 'Inventory.weight_in'と' Inventory.weight_out'にインクリメントを使います。私はあなたに尋ねたい、それは良いですか?または、私は(forループを使って)再カウントを行い、 'Inventory.weight_in'と' Inventory.weight_out'の値を置き換える必要がありますか? -ありがとうございます。 –

+0

'post_save'は' created'パラメータを持ち、その値が 'True'の場合は新しいレコードが作成され、それ以外の場合はレコードが編集されました。 –

+0

'weight_received'の値を計算するための集計を改善しました:' weight_received = Received.objects.aggregate(Sum( 'weight')) ' –

関連する問題