2014-10-30 24 views
0

我有一個基於類的觀點是這樣的:郵政API在Django的基於類的視圖保存到數據庫之前

class UpdateProfileView(UpdateView): 
    model = models.Profile 
    form_class = forms.ProfileForm 
    template_name = 'userprofile/add_user_profile.html' 
    success_url = '/' 

而且我想補充的方式,收到了數據更新到數據庫中它也將數據更新爲自定義API,但我對django基於類的視圖瞭解不多。

在將數據發送到數據庫之前,我該如何將它掛鉤我獲取表單數據並將其發佈到API中?

回答

1

聽起來像覆蓋form_valid()將是一個好方法。

def form_valid(self, form): 
    post_to_api(form.cleaned_data) 
    # now call superclass to save the form 
    return super(UpdateProfileView, self).form_valid(form) 
+0

我覺得'save()'更合適,因爲發佈到API也是一種保存功能。 – 2014-10-30 13:19:42

+0

像魅力一樣工作,謝謝! – 2014-10-30 14:24:43