Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 23, 2010 12:42:34

ozgur y.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Model Formset Pre-Filled Problem


Hi,

I'm trying to use model formsets. At first, everything is normal. But when i
open the form page for the second time, i see that the formset is pre-filled
with the previous values. 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

class CooModel( models.Model ):

c_field = models.CharField( blank=True,null=True,max_length = 100 )

def __unicode__( self ):
return u"%s" % ( smart_unicode(self.c_field))



forms.py
-------------------------------
from django.forms.models import modelformset_factory

FooFormSet = modelformset_factory(FooModel)

class CooModel_Form(ModelForm):

class Meta:
model = CooModel


views.py
-------------------------------
def new_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('yeni_alis_irsaliyesi.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>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 17th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support