好吧,伙计们,我被困住了。
我遇到的问题如下:
我有两个模型,RelenvaceCodingRecord
和EventRecord
用户应该能够选择文章是否相关。如果是,则表格的其他字段应该是必需的,如果不是=没有必填字段。
这必须在一个表单中,在同一页面上,没有JS。
到目前为止,我所做的是创建一个包含两个模型的许多字段的表单(forms.Form)。在this篇文章之后,我尝试实现条件required
逻辑:
class CodingForm(forms.Form):
relevance = forms.BooleanField(required=False)
event_date = forms.DateField('Event Date',required=False)
location = forms.ModelChoiceField(queryset=Location.objects.all(), empty_label=None, required=False)
actors = forms.CharField(max_length=100, required=False)
party = forms.NullBooleanField('Party')
labor_org = forms.NullBooleanField('Labor Organization')
property_dmg = forms.NullBooleanField('Property Damage')
violence = forms.NullBooleanField('Violence')
injured = forms.IntegerField(required=False)
deaths = forms.IntegerField(required=False)
participants_num = forms.IntegerField(required=False)
participants_str = forms.IntegerField(required=False)
anti_gov = forms.NullBooleanField('Anti Government')
presence = forms.NullBooleanField('Presence')
intervene = forms.NullBooleanField('Intervention')
brutality = forms.NullBooleanField('Brutality')
arrests = forms.NullBooleanField('Arrests')
issue = forms.CharField(max_length=100,required=False)
url = forms.CharField(max_length=100,required=False)
def __init__(self, data=None, *args, **kwargs):
super(CodingForm, self).__init__(data, *args, **kwargs)
if data and data.get('relevance', None) == True:
self.fields['event_date'].required = True
self.fields['location'].required = True
self.fields['anti_gov'].required = True
在我看来,然后,我尝试获取表单并将其值传递给RelevanceCodingRecord
和EventRecord
的两个ModelForms并将其全部保存:
if request.method == 'POST':
if 'coding' in request.POST:
event_form = EventRecordForm(request.POST, prefix='event')
relevance_form = RelevanceCodingRecordForm(request.POST, prefix='event')
coding_form = CodingForm(request.POST, prefix='coding')
if coding_form.is_valid():
obj = coding_form.save(commit=False)
event = event_form.save(commit=False)
relevance = relevance_form.save(commit=False)
# Relevance Data
relevance.relevance = obj.relevance
relevance.screened = True
relevance.classified = True
relevance.coder = request.user.coder
relevance.article = paginator.page(page).object_list[0]
relevance.last_updated = datetime.datetime.today
relevance.save()
if relenvace.relevance == True:
# Event Data
event.article = paginator.page(page).object_list[0]
event.event_date = obj.event_date
event.country = paginator.page(page).object_list[0].country
event.location = obj.event_location
event.actors = obj.actors
event.party = obj.party
event.labor_org = obj.labor_org
event.property_dmg = obj.property_dmg
event.violence = obj.violence
event.injured = obj.injured
event.deaths = obj.deaths
event.participants_num = obj.participants_num
event.participants_str = obj.participants_str
event.anti_gov = obj.anti_gov
event.presence = obj.presence
event.intervene = obj.intervene
event.brutality = obj.brutality
event.arrests = obj.arrests
event.issue = obj.issue
event.url = paginator.page(page).object_list[0].source
event.coder = request.user.coder
event.last_updated = datetime.datetime.today
event.save()
redirect_to = "?page=%s" % page
return HttpResponseRedirect(redirect_to)
else:
redirect_to = "?page=%s" % page
return HttpResponseRedirect(redirect_to)
else:
coding_form = CodingForm(prefix='coding')
coding_form.fields["location"].queryset = Location.objects.filter(ccode=country).order_by('name')
然而,这不起作用:我的数据库中没有创建记录,我只被重定向 有没有办法让这项工作完全成功?或者我在这里傻瓜差事? 从一种形式传递到另一种形式的大多数数据也不是DRY,但我不知道更好的方法。
非常感谢任何帮助!
答案 0 :(得分:1)
由于coding_form
不是ModelForm的实例,因此coding_form.save()
将不起作用,并且不会返回模型实例。您可以使用coding_form.cleaned_data
字典访问表单数据。