2013-03-22 91 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提到的可能是一个更好的方式去了解这个