Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango 'ManagementForm data is missing or has been tampered with' when saving modelForms with foreign key link
    text
    copied!<p>I am rather new to Django so this may be an easy question. I have 2 modelForms where there is a ForeignKey to another. My main goal is to save Indicators with a link to Disease (FK), such that for a particular disease, you can have multiple indicators. </p> <p>With the code below, I get an error when I hit submit that says 'ManagementForm data is missing or has been tampered with'. Also, the code in views.py does not seem to be validating at the 3rd 'if' statement where there is a return HttpResponseRedirect. However, when I check my database, the values from the form have been written. Any ideas on why the error has been raised? and how to fix it?</p> <p>My code is below:</p> <p>models.py</p> <pre><code>#Table for Disease class Disease(models.Model): disease = models.CharField(max_length=300) #Tables for Indicators class Indicator(models.Model): relevantdisease = models.ForeignKey(Disease) indicator = models.CharField(max_length=300) </code></pre> <p>forms.py</p> <pre><code>class DiseaseForm(forms.ModelForm): class Meta: model = Disease class IndicatorForm(forms.ModelForm): class Meta: model = Indicator DiseaseFormSet = inlineformset_factory(Disease, Indicator, can_delete=False, form=DiseaseForm) </code></pre> <p>views.py</p> <pre><code>def drui(request): if request.method == "POST": indicatorForm = IndicatorForm(request.POST) if indicatorForm.is_valid(): new_indicator = indicatorForm.save() diseaseInlineFormSet = DiseaseFormSet(request.POST, request.FILES, instance=new_indicator) if diseaseInlineFormSet.is_valid(): diseaseInlineFormset.save() return HttpResponseRedirect('some_url.html') else: indicatorForm = IndicatorForm() diseaseInlineFormSet = DiseaseFormSet() return render_to_response("drui.html", {'indicatorForm': indicatorForm, 'diseaseInlineFormSet': diseaseInlineFormSet},context_instance=RequestContext(request)) </code></pre> <p>template.html</p> <pre><code> &lt;form class="disease_form" action="{% url drui %}" method="post"&gt;{% csrf_token %} {{ indicatorForm.as_table }} &lt;input type="submit" name="submit" value="Submit" class="button"&gt; &lt;/form&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload