在Django UpdateView中进行表单编辑

时间:2016-06-21 17:17:03

标签: python django

我试图改变形式,就像它的标签一样,可能更多。 我使用UpdateView的get_form方法来改变表单,但它不起作用

views.py

class SystemUpdateView(SuccessMessageMixin, PermissionRequiredMixin, UpdateView):
"""CRUD update view for :class:`~.System` objects"""

form_class = SystemForm
model = System
permission_required = 'services.change_system'
success_message = "System '%(name)s' updated successfully."
template_name = 'systems/system_form.html'

# when system edited need to modify accountable_team_history

def form_valid(self, form):

    system = self.get_object()

    if form.instance.accountable_team is not None:
        if system.accountable_team == form.instance.accountable_team:
            pass
        else:
            try:
                old_team_history = system.accountable_team_history.get(end_date__isnull=True)

                old_team_history.end_date = datetime.datetime.now()

                old_team_history.save()
            except AccountableTeamHistory.DoesNotExist:
                pass

            new_history = AccountableTeamHistory(resource=system,
                                         accountable_team=form.instance.accountable_team,
                                         effective_date=datetime.datetime.now())

            system.accountable_team_history.add(new_history)

            system.save()

def get_form(self, form_class=SystemForm):

    form_class.fields['primary_purpose_business_use'].label = "Primary purpose/business use"
    form_class.fields['secondary_purpose_business_uses'].label = "Secondary purpose/business uses"

    return form_class

从forms.py导入SystemForm我不认为get_form方法现在正在做任何事情。什么可能是错的?

1 个答案:

答案 0 :(得分:2)

您需要实例化form_class的实例并将其返回。 See the documentation

unknown command