Alright guys, I am stuck.
The problem I have is as follows:
I have two Models, RelenvaceCodingRecord
and EventRecord
.
The user should be able to select if an article is relevant. If it is, other fields of the form should be required, if it is not = no required fields.
This has to happen in one form, on the same page, without JS.
What I did so far is to create a form (forms.Form) that has many of the fields of the two models. Following this article, I tried to implement the conditional required
logic:
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
In my view, then, I tried to get the form and pass its values to two ModelForms of the RelevanceCodingRecord
and the EventRecord
and save it all:
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')
This, however, does not work: No Records are created in my database, I only get redirected.
Is there a way to make this work, at all? Or am I on a fools errand here?
Most of the data passing from one form to the other is not DRY as well, but I don't know a better way.
Any help here is greatly appreciated!
Since coding_form
is not an instance of ModelForm and hence coding_form.save()
will not work and will not return a model instance. You can access form data by using coding_form.cleaned_data
dictionary.