Django 'ManagementForm data is missing or has been tampered with' when saving modelForms with foreign key link
Asked Answered
Q

1

11

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.

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?

My code is below:

models.py

#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)

forms.py

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)

views.py

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))   

template.html

 <form class="disease_form" action="{% url drui %}" method="post">{% csrf_token %}
  {{ indicatorForm.as_table }}
 <input type="submit" name="submit" value="Submit" class="button">
 </form>
Quita answered 2/8, 2013 at 17:27 Comment(1)
C
26

You have neither diseaseFormSet nor diseaseFormSet's management form in your template, yet you try to instantiate the formset. Formsets require the hidden management form which tells django how many forms are in the set.

Insert this into your HTML

{{ diseaseFormSet.as_table }} 
{{ diseaseFormSet.management_form }}
Cricket answered 2/8, 2013 at 17:38 Comment(4)
I still get the same error. If I take out {{indicatorForm.as_table}} and add the 2 lines above, my form shows up completely blank. When I keep {{indicatorForm.as_table}} and add your 2 lines above {indicatorForm}}, my form shows up but raises the 'management data is missing error'.Quita
Actually, I'm assuming you mean {{diseaseInlineFormSet.management_form}} since I am passing that to render_to_response. Now my error is "global name 'diseaseInlineFormset' is not defined". I'm not sure where I should define this variable...Quita
Played around with it more! It works. There was a typo in my view.py where it should say diseaseInlineFormSet and not diseaseInLineFormset. Also, passing the {{diseaseInlineFormSet.management_form}} was the real problem. Thanks!Quita
I'm struggling with something similar--I'm reading through this question and it seems to me that this will generate one indicator and many diseases. Am I totally backwards?Leporid

© 2022 - 2024 — McMap. All rights reserved.