python - Save Data from Django Model Formset -


how save data django model formset database?

models.py:

from django.db import models django.utils import timezone  class mymodel(models.model):     idno = models.charfield(max_length=20)     date = models.datetimefield(default=timezone.now)     entity = models.charfield(max_length=50)     logic = models.charfield(max_length=100)     choices = (         ('1', 'choice1'),         ('2', 'choice2'),         ('3','choice3'),     )     choices = models.charfield(         max_length=20,         choices=choices,         null=true,         )     comment = models.charfield(max_length=500, null=true)     def __str__(self):         return self.idno 

forms.py:

from .models import mymodel django.forms import modelformset_factory, modelform  class myform(modelform):      class meta:         model = mymodel         fields = '__all__'      def __init__(self, *args, **kwargs):         super(myform, self).__init__(*args, **kwargs)         self.fields['idno'].disabled = true         self.fields['date'].disabled = true         self.fields['entity'].disabled = true         self.fields['logic'].disabled = true  myformset = modelformset_factory(mymodel, extra=1, exclude=(), form=myform) 

views.py:

from django.shortcuts import render django.http import httpresponse .models import mymodel .forms import myformset  def index(request): new = mymodel.objects.filter(choices__isnull=true) modelformset = myformset(request.post or none, queryset=new) context = {'modelformset':modelformset} if request.method == 'post':     if modelformset.is_valid():         modelformset.save()         idno = modelformset.cleaned_data['idno']         entity = modelformset.cleaned_data['entity']         messages.success(request, '%s %s submitted' % (idno, entity))     return httpresponseredirect('/') return render(request, 'selfserve/index.html', context) 

index.html:

<form method="post" action="">   {% csrf_token %}   {{ modelformset.management_form }} <table> <tr>   <th>idno</th>   <th>date</th>   <th>entity</th>   <th>logic</th>   <th>choices</th>   <th>comment</th> </tr> {% form in modelformset %} <tr>   <td>{{ form.idno }}</td>   <td>{{ form.date }}</td>   <td>{{ form.entity }}</td>   <td>{{ form.logic }}</td>   <td>{{ form.choices }}</td>   <td>{{ form.comment }}</td>   <td><input type="submit" value="submit"></td> </tr> {% endfor %} </table> </form> 

right now, returning nice table of each element in model formset submit button next each one. when fill in enabled fields (choices , comment), form within formset saved database. right now, when click 1 of submit buttons, error occurs:

['managementform data missing or has been tampered with'] 

i know means management form wasn't included in template. however, have included piece.

i think issue in combination of views.py , index.html, i'm not sure where. ideally, data saved, , user stays on same page don't have go , forth save each one. or, work have 1 save button click after finished making edits.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -