Django Model Formset Pre-Filled Value Problem

Posted by user552377 on Stack Overflow See other posts from Stack Overflow or by user552377
Published on 2010-12-23T12:51:08Z Indexed on 2010/12/23 12:54 UTC
Read the original article Hit count: 230

Hi, i'm trying to use model formsets with Django. When i load forms template, i see that it's filled-up with previous values. Is there a caching mechanism that i should stop, or what?

Thanks for your help, here is my code:

models.py

class FooModel( models.Model ):

    a_field     = models.FloatField()
    b_field     = models.FloatField()

    def __unicode__( self ):
        return self.a_field

forms.py

from django.forms.models import modelformset_factory

FooFormSet = modelformset_factory(FooModel)

views.py

def foo_func(request):

    if request.method == 'POST':

        formset = FooFormSet(request.POST, request.FILES, prefix='foo_prefix' )

        if formset.is_valid():

            formset.save()

            return HttpResponseRedirect( '/true/' )

        else:

            return HttpResponseRedirect( '/false/' )

    else:

        formset = FooFormSet(prefix='foo_prefix')

    variables = RequestContext( request , { 'formset':formset , } )

    return render_to_response('footemplate.html' , variables )

template:

<form method="post" action=".">
{% csrf_token %}
    <input type="submit" value="Submit" />
   <table id="FormsetTable" border="0" cellpadding="0" cellspacing="0">
       <tbody>
           {% for form in formset.forms %}
           <tr>
              <td>{{ form.a_field }}</td>
              <td>{{ form.b_field }}</td>
           </tr>
           {% endfor %}
       </tbody>
   </table>
   {{ formset.management_form }}
</form>

© Stack Overflow or respective owner

Related posts about django

Related posts about forms