表格清洁和处理不起作用 - Django

时间:2013-06-28 13:35:32

标签: django forms validation view

好吧,这可能是一个非常明显的问题,我只是没有看到它,但我需要帮助:

在以下视图中,我处理一个formset。 如果用户填写表单,我希望保存EventRecordArticleHistory,如果用户将表单保留为空,我只想保存 ArticleHistory

def process_form(formset, request, current_page, paginator):
    if formset.is_valid():
        for form in formset.forms:
            form = form.cleaned_data

            ##### Check if user filled the form
            if form["relevance"] == False:
                pass
            elif form["relevance"] == True:

                ##### If user filled the form, save EventRecord

                event_form = EventRecordForm()

                event = event_form.save(commit=False)
                event.article = paginator.page(current_page).object_list[0]
                event.coder = request.user.coder
                event.last_updated = datetime.datetime.today()
                event.event_date = form["event_date"]
                event.location = form["location"]
                event.actors = form["actors"]
                event.num_participants = form["num_participants"]
                event.issue = form["issue"]
                event.side = form["side"]
                event.scope = form["scope"]
                event.part_violence = form["part_violence"]
                event.sec_engagement = form["sec_engagement"]
                event.save()

        ##### Add info on who worked on the article when
        history_form = ArticleHistoryForm()
        article_history = history_form.save(commit=False)
        article_history.article = paginator.page(current_page).object_list[0]
        article_history.coder = request.user.coder
        article_history.last_updated = datetime.datetime.now()
        article_history.save()

我尝试通过清理表单来做到这一点:

class CodingForm(forms.Form):

    event_date = forms.DateField(required=False)
    location = forms.ModelChoiceField(queryset=Location.objects.all(), required=False)
    actors = forms.CharField(max_length=100, required=False)
    num_participants = forms.CharField(max_length=200, required=False)  
    issue = forms.CharField(max_length=200, required=False)
    side = forms.NullBooleanField('Side')
    scope = forms.TypedChoiceField(choices=SCOPE_CHOICES, coerce=int, empty_value=None)
    part_violence = forms.TypedChoiceField(choices=PART_VIO, coerce=int, empty_value=None)
    sec_engagement = forms.TypedChoiceField(choices=SEC_ENG, coerce=int, empty_value=None)
    relevance = forms.NullBooleanField('Relevance')

    def clean(self):
        cleaned_data = self.cleaned_data
        relevance = cleaned_data.get("relevance")
        event_date = cleaned_data.get("event_date")
        location = cleaned_data.get("location")

        if event_date and location:
            relevance = True
        else:
            relevance = False

        return cleaned_data

但是,如果我这样做 - 无论我做什么 - 只保存ArticleHistory而不保存EventRecord
我错过了什么?

1 个答案:

答案 0 :(得分:2)

clean方法中,您正在设置一个名为relevance但未对其执行任何操作的局部变量。我希望您将cleaned_data['relevance']设置为True或False。