在def post下以Django形式默认一个updateview操作

时间:2018-12-20 20:35:12

标签: django django-forms django-crispy-forms

与项目中的另一个编码器一起工作。他的更改使某些表单上的UpdateView停止了保存修改。我知道为什么。...他定义了

def post

适用于他正在处理的情况,但需要执行默认更新的else操作。当他UpdateView不能自动完成所有操作时,我不确定该怎么做。

UpdateView的代码:

class ProviderUpdateView(UpdateView):                                                                                             
  model = Provider                                                                                                                
  form_class = ProviderForm                                                                                                       
  provider_form_class = ProviderForm                                                                                                       
  provider_term_form_class = ProviderTermForm                                                                                                       
  template_name = 'ipaswdb/provider/provider_form.html'                                                                           
  success_url = '/ipaswdb/provider/'                                                                                               

  def get_context_data(self, **kwargs):
    context = super(ProviderUpdateView, self).get_context_data(**kwargs)

    provider = context['object'] 
    context['provider_id'] = provider.id
    prov = Provider.objects.get(pk=provider.id)

    #print "provider: ",
    #print prov
    #print "provider terminated: ",
    #print prov.is_terminated

    if prov.is_terminated:
      provider_form = ProviderFormView(instance=prov)
      context['readonly'] = True 
    else:
      print("NOT TERMINATED SO LETS DO THIS")
      provider_form = ProviderForm(instance=prov)
      context['readonly'] = False 

    context['provider_form'] = provider_form 

    provider_term_form = ProviderTermForm()
    context['provider_term_form'] = provider_term_form 

    ### Get the list of GroupLocations, but remove the current one this provider is associated with
    ### I just need the grouplocation id and the name
    #grplocs = GroupLocationProvider.objects.filter(

    return context


  def post(self, request, *args, **kwargs):
    #print self.request.POST.keys()
    #print self.request.POST.values()
    print("Posting...")
    if self.request.POST.has_key('terminate'):
      provider = Provider.objects.get(pk=kwargs['pk'])
      form = ProviderTermForm(request.POST)
      if form.is_valid():
        print "Terminating Provider: ",
        print provider

        provider_term = ProviderTerm()

        provider.is_terminated = True
        provider.save()

        ### Update the term fields
        provider_term.provider = provider
        provider_term.old_id = provider.id
        provider_term.term_date = form.cleaned_data['term_date']
        provider_term.term_comment = form.cleaned_data['term_comment']
        provider_term.save()

    return HttpResponseRedirect(self.success_url) 

我知道我在帖子中还需要其他说明:

if self.request.POST.has_key('terminate'):

我只是不确定UpdateView中什么是您常规的工作。我检验了我的假设,即他的代码破坏了编辑和保存提供程序的能力,因为我完全删除了def帖子,并且在UpdateView automagic上都工作良好。既然我们压倒一切? def post在我看来,我们必须自己处理常规更新,只是不确定其外观如何。

0 个答案:

没有答案