好吧,伙计们,我被困住了。
我遇到的问题如下:我有两个模型,RelenvaceCodingRecord
并且EventRecord
.
用户应该能够选择文章是否相关。如果是,则应要求表单的其他字段,如果不是 = 没有必填字段。这必须以一种形式发生,在同一页面上,没有 JS。
到目前为止,我所做的是创建一个表单(forms.Form),其中包含两个模型的许多字段。在这篇文章之后,我尝试实现条件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
在我看来,然后,我尝试获取表单并将其值传递给两个 ModelFormsRelevanceCodingRecord
和 theEventRecord
并将其全部保存:
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,但我不知道更好的方法。
非常感谢这里的任何帮助!