2013-03-22 63 views
0

我想要一種方法來在我的流程完成時更新模型字段中的狀態。Django從模型中更新狀態

views.py

#If we had a POST then get the request post values. 
    if request.method == 'POST': 
     batches = Batch.objects.for_user_pending(request.user) 
     for batch in batches: 
      ProcessRequests.delay(batch) 

所以我想在視圖中做這樣的事情的......

batch.complete_update() 

我的問題是,在我的模型因爲我不知道如何,只需要一點幫助。

這是我迄今所做的......

我創建

STATUSES = (
    ('Pending', 'Pending'), 
    ('Complete', 'Complete'), 
    ('Failed', 'Failed'), 
    ('Cancelled', 'Cancelled'), 
) 

然後模型函數調用def complete_update(self):,但我不知道如何使用更新它的領域狀態,然後將其全部保存在模型中。

預先感謝您。

PS,這是正確的方式去呢?

回答

1
class Batch(model.Model): 
    STATUSES_CHOICES = (
     ('Pending', 'Pending'), 
     ('Complete', 'Complete'), 
     ('Failed', 'Failed'), 
     ('Cancelled', 'Cancelled'), 
    ) 
    status = models.CharField(max_length=25, choices=STATUS_CHOICES) 
    # The rest of the fields.. 

    def complete_update(self): 
     self.status = 'Complete' 
     self.save() 

應該這樣做

編輯:由於karthikr一個post_save提到的可能是一個更好的方式去了解這個