2

I'm trying to create an editor for a question whereas the question can have multiple files associated with it. I've tried to get it to work with inline_formsets but I can't seem to get it working properly. The form I'm trying to create is for a Question whereas I want the user to be able to upload files for the Question on the same form.

models.py

def Question(models.Model):
  title = models.CharField('Title', max_length=200)
  ...

def AdditionalData(models.Model):
  question = models.ForeignKey(Question, related_name='additionalData`, on_delete=models.CASCADE)
  upload = models.FileField('Data', upload_to=...)

forms.py

class QuestionForm(forms.ModelForm)
   class Meta:
      model = Question
      fields = '__all__'  # for example purposes only

 AdditionalDataFormset = forms.inline_formset_factory(models.Question, models.AdditionalData, fields=('upload',), extra=1)

views.py

class EditQuestion(DetailsView):
    def get(request, pk, **kwargs):
        question = get_object_or_404(Question, pk=pk)
        question_form = QuestionForm(instance=question)
        additional_data_formset = AdditionalDataFormset(instance=question)

        context = {
              'question_form': question_form,
              'additional_data_formset': additional_data_formset
        }
         return render(request,'editor.html', context)

Lastly, part of my template editor.html

 <form method="post" action='url...'>
       {% csrf_token %}
       <fieldset>
          {{ question_form|crispy }}
       </fieldset>

       {{ additional_data_formset.management_form }}
       {{ additional_data_formset.non_form_errors }}
       <fieldset>
         {% for additional_data in additional_data_formset %}
               {{ additional_data|crispy }}
         {% endfor %}
       </fieldset>

       <button type="submit" class="save btn btn-default" value="Submit">Submit</button>
 </form>

For some reason when I'm using inline formsets the "Submit" button does not work (nothing happens when I click it). When I attempt to force submit the form (via jquery onclick submit button) this error is thrown:

'ManagementForm data is missing or has been tampered with'

I can't seem to figure out why that's happening. As for the nesting method, I have been unable to find an example that demonstrates it with just two models, so I am unsure of how to use it in this circumstance. As you can see above I made the call to the "management_form" perfectly so I'm not sure why this is happening.

Additional Information: A question can have any number of files (AdditionalData instances) associated with it.

Any information on the above error or tips on how to implement a form that can have a variable number of subforms would be greatly appreciated.

  • Possible duplicate of [ManagementForm data is missing or has been tampered with](https://stackoverflow.com/questions/25331500/managementform-data-is-missing-or-has-been-tampered-with) – scharette Jun 18 '18 at 17:27
  • @scharette That appears to be a bit different. That person is only using one formset (I'm using one form along with one formset) and OP did not include the call to .management_form whereas I did. I believe our situations/questions are sufficiently different. – steven landau Jun 18 '18 at 17:29

0 Answers0