[Django] #24482: Initializing Grouped model Form does not work

10 views
Skip to first unread message

Django

unread,
Mar 14, 2015, 6:35:03 AM3/14/15
to django-...@googlegroups.com
#24482: Initializing Grouped model Form does not work
-----------------------------+---------------------------------------------
Reporter: Drife59 | Owner: nobody
Type: | Status: new
Uncategorized |
Component: | Version: 1.7
Uncategorized |
Severity: Normal | Keywords: GroupedModelForm Initialization
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+---------------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/24482>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 14, 2015, 6:40:53 AM3/14/15
to django-...@googlegroups.com
#24482: Initializing Grouped model Form does not work
-------------------------------------+-------------------------------------
Reporter: Drife59 | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.7
Severity: Normal | Resolution:
Keywords: GroupedModelForm | Triage Stage:
Initialization | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by bmispelon):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Hi,

Shouldn't `formset = LigneProduitFormSet2(initial)` be `formset =
LigneProduitFormSet2(initial=initial)` instead?

--
Ticket URL: <https://code.djangoproject.com/ticket/24482#comment:1>

Django

unread,
Mar 14, 2015, 10:56:39 AM3/14/15
to django-...@googlegroups.com
#24482: Initializing Grouped model Form does not work
-------------------------------------+-------------------------------------
Reporter: Drife59 | Owner: nobody

Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.7
Severity: Normal | Resolution:
Keywords: GroupedModelForm | Triage Stage:
Initialization | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Drife59):

Yes you are right.

I guess I need more coffee.

Thank you for your help.

--
Ticket URL: <https://code.djangoproject.com/ticket/24482#comment:2>

Django

unread,
Mar 14, 2015, 10:57:31 AM3/14/15
to django-...@googlegroups.com
#24482: Initializing Grouped model Form does not work
-------------------------------------+-------------------------------------
Reporter: Drife59 | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 1.7
Severity: Normal | Resolution: wontfix

Keywords: GroupedModelForm | Triage Stage:
Initialization | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Drife59):

* status: new => closed
* resolution: => wontfix


--
Ticket URL: <https://code.djangoproject.com/ticket/24482#comment:3>

Reply all
Reply to author
Forward
0 new messages