On Fri, Mar 8, 2013 at 4:42 AM, carlos <crocha09...@gmail.com> wrote:
>
> Hi
> is posible fill forms with initial values for example
>
> ------ models.py ---------
> class Model1(models.Model):
>     field1 = models.CharField()
>     field2 =models.CharField()
>
> CHOICE_ONE = (
>       (1,'one'),
>       (2, 'two'),
>       (3, 'three'),
>       (4, 'four')
> )
> class Model2(models.Model):
>      field_fk = fk(Model1)
>      field2 = models.IntegerField(choices=CHOICE_ONE)
>      field3 = IntegerField()
>      field4 = IntegerField()
>
> ----- forms.py -----------
>
> class Model2Form(forms.ModelForm):
>
>     class Meta:
>         model = Model2
>         exclude = ('field_fk',)
>
> ---------- views.py --------------
>
> def name_views(request):
>     initial_one = [{'field2':'one'},
>                         {'field2':'two'},
>                         {'field2':'three '},
>                         {'field2':' four'},
>                        ]
>     form1FormSet = modelformset_factory(Model2, formModel2Form=,extra=4)
>     if request.method == "POST":
>         form1 = Model2Form(request.POST)
>         if form1.is_valid():
>             form1.save()
>    else:
>         form1 = form1FormSet(initial=initial_one)
>
>     return render_to_response('template_form1.html',{'form1':form1},
>                               context_instance=RequestContext(request))
>
> ------ template_form1.html --------
>
>        {% csrf_token %}
>       {{form1.management_form}}
>
>       {%for fila in form1.forms%}
>                {{ fila }}<br>
>       {%endfor%}
>
> but this no working the output is
>
>
> but i need to appear so
>
>
>
> the words in red are the options
>
> which is the right way to do better and if possible do that
>
> additionally I read this but it's not like my model
> https://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-initial-data-with-a-formset
>
> Cheers

Your initial values possibly don't appear because they are not valid
choices for that field - you've given it the text value of the choice,
it is expecting the data value, eg 1, not 'One'.

Additionally, your formset handling appears to be incorrect. Before
submission, you create an instance of your formset, yet after
submission you create an instance of your model form to try and save
it.

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to