わかりました、これはおそらく非常に明白な問題であり、私はそれを見ていないだけですが、助けが必要です:
次のビューでは、フォームセットを処理しています。ユーザーがフォームに入力した場合は保存EventRecord
しArticleHistory
、ユーザーがフォームを空のままにした場合は、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
。
私は何が欠けていますか?