在基于django类的视图中保存到数据库之前发布到API

时间:2014-10-30 12:44:30

标签: python django django-views

我有一个基于类的视图:

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

我想添加一种方法,在将数据更新到数据库之前,它还会将数据更新为自定义API,但我对django基于类的视图了解不多。

如何在将数据发送到数据库之前将其连接起来我获取表单数据并将其发布到我拥有的API中?

1 个答案:

答案 0 :(得分: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)