2010-12-20 67 views
3

我剛開始學習Django,我有一個問題。如何覆蓋modelform類的save()方法並添加缺少的信息?

我試圖在保存表單數據時自動添加缺少的信息。我可以通過覆蓋modelform類的save()方法來更改/添加所需的「cleaned_data」信息,但是更改不會記錄在數據庫中。其實,如何編寫修改後的信息?這是代碼:

def save(self, commit = True, *args, **kwargs): 
    temp = ServiceMethods(url = self.cleaned_data.get('url'), wsdl_url = self.cleaned_data.get('wsdl_url')) 

    if not temp.get_wsdl_url():        
     temp.make_wsdl_url() 

    if temp.get_wsdl_url():         
     temp.make_wsdl()          
     self.cleaned_data['wsdl_url'] = temp.get_wsdl_url() 
     self.cleaned_data['wsdl_description'] = temp.get_wsdl_description() 

    super(ServiceForm, self).save(commit = commit, *args, **kwargs) 

和型號:

class Services(models.Model): 
    name    = models.CharField('Имя', max_length=256) 
    url     = models.URLField('Ссылка', unique = True) 
    wsdl_url   = models.URLField('Ссылка на WSDL-документ', blank=True) 
    description   = models.TextField('Описание сервиса',blank=True) 
    wsdl_description = models.TextField('WSDL описание', blank=True, editable=False) 
    added    = models.DateTimeField('Добавлено', auto_now_add=True) 

TIA

回答

2

嘗試設置上self.instance,而不是在self.cleaned_data的數據,並讓我知道是否可行。

+0

是的,它的工作原理!非常感謝! – User 2010-12-20 21:16:14