﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24482	Initializing Grouped model Form does not work	Drife59	nobody	"Hi, 

I wanted to initialized a grouped model form using datas in session. 

Here is my code commented: 

{{{
        LigneProduitFormSet2 = modelformset_factory(ProduitCommande, extra=1)

        initial=[]
        #Just for the test, get only the first item from Session, id and qte
        id_produit, quantite = panier.panier.popitem()
         
        #Get corresponding product object
        produit = Produit.objects.get(id=id_produit)
        #Product object has a ManyToManyField relation to Cuisson, getting corresponding object
        cuissons = produit.cuisson_dispo.all()
        #Creating corresponding object for initialization
        ligne_produit = {""produit"": produit, ""cuisson"":cuissons, ""quantite"": quantite, 
                         ""prix_unitaire"":produit.prix, ""prix_ligne"": (produit.prix * float(quantite))}
        initial.append(ligne_produit)
         
        # This is working 
        formset = LigneProduitFormSet2(initial)
         
        #This is breaking everything
        for form in formset:
            pass
            #print(form.as_table())
}}}

If my understanding of the documentation is correct, I should be able to initialize the data like this. 
"" ''As with regular formsets, it’s possible to specify initial data for forms in the formset by specifying an initial parameter when instantiating the model formset class returned by modelformset_factory()'' ""

If I delete the initial parameter, my code is working, even my template is properly generated. 
the loading from tha database is also working. 

Here is the ful stacktrace: 


{{{
Traceback:
File ""/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py"" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File ""/usr/local/lib/python3.4/dist-packages/django/contrib/auth/decorators.py"" in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)
File ""/home/drife/projet/crepes_bretonnes/blog/views.py"" in commande_boulangerie
  166.         for form in formset:
File ""/usr/local/lib/python3.4/dist-packages/django/forms/formsets.py"" in __iter__
  72.         return iter(self.forms)
File ""/usr/local/lib/python3.4/dist-packages/django/utils/functional.py"" in __get__
  55.         res = instance.__dict__[self.func.__name__] = self.func(instance)
File ""/usr/local/lib/python3.4/dist-packages/django/forms/formsets.py"" in forms
  141.         forms = [self._construct_form(i) for i in xrange(self.total_form_count())]
File ""/usr/local/lib/python3.4/dist-packages/django/forms/formsets.py"" in total_form_count
  114.             return min(self.management_form.cleaned_data[TOTAL_FORM_COUNT], self.absolute_max)
File ""/usr/local/lib/python3.4/dist-packages/django/forms/formsets.py"" in management_form
  93.             if not form.is_valid():
File ""/usr/local/lib/python3.4/dist-packages/django/forms/forms.py"" in is_valid
  162.         return self.is_bound and not bool(self.errors)
File ""/usr/local/lib/python3.4/dist-packages/django/forms/forms.py"" in errors
  154.             self.full_clean()
File ""/usr/local/lib/python3.4/dist-packages/django/forms/forms.py"" in full_clean
  353.         self._clean_fields()
File ""/usr/local/lib/python3.4/dist-packages/django/forms/forms.py"" in _clean_fields
  362.             value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File ""/usr/local/lib/python3.4/dist-packages/django/forms/widgets.py"" in value_from_datadict
  228.         return data.get(name, None)

Exception Type: AttributeError at < my url>
Exception Value: 'list' object has no attribute 'get'
}}}

So I had a closer look on what want wrong, 

Exception Value: 'list' object has no attribute 'get'
File ""/usr/local/lib/python3.4/dist-packages/django/forms/widgets.py"" in value_from_datadict
  228.         return data.get(name, None)

using some print: 

{{{
        print(""data : %s "" % data)
        print(""name : %s "" % name)
}}}

Here is what I got: 

{{{
data : [{'prix_ligne': 4.75, 'produit': <Produit: Baguette de Boulangerie Bêle >, 'prix_unitaire': 0.95, 'cuisson': [<CuissonDispo: Pas trop cuit>, <CuissonDispo: Standard>, <CuissonDispo: Bien cuit>], 'quantite': 5}] 
name : form-TOTAL_FORMS
}}}

So we are trying to do a ""get"" on object data which is a list... Here is the reason. 

So, 

Is there something very wrong about my initialization ?
Is there a nasty bug is my particular case ? 

Regards. 





  "	Uncategorized	closed	Uncategorized	1.7	Normal	wontfix	GroupedModelForm Initialization	bjbtiben@…	Unreviewed	0	0	0	0	0	0
