Friday, October 7, 2011

Passing parameters to form in formset

Hello,

* Short version:

How can I build a formset that creates so many form instances that a
result set has records and passes one record from the result set to each
form constructor?


* Long version:

I am building a browser game in Django. I have a GameBase that can have
multiple DefendingArmy records. DefendingArmy model represents suppoprting
units from other game bases. When a player send units to multiple bases,
he can draw some units back. He can select from every base how many units
he want to recall. So I need to display a form, where every row represents
DefendingBase record and every column is an amount of units of given unit
type he wants to recall.

So I wanted to create a form, where I pass DefendingArmy record and from
this record I dynamically create fields (dynamically because of a
validation for unit counts - or I can create for each field clean_field
method instead if I have an access to the record). Here I have two
problems:

1.) If I create a formset, I have to pass DefendingArmy record for every
form instance but I don't know how. I tried this solution
(http://stackoverflow.com/questions/622982/django-passing-custom-form-parameters-to-formset)
but I end with this error:

da = DefendingArmy.objects.get(id=1)
formset = formset_factory(RecallUnitsForm)
formset.form = staticmethod(curry(RecallUnitsForm, defending_army=da)

>>> formset.forms

AttributeError: type object 'RecallUnitsFormFormSet' has no attribute
'forms'


2.) How to take for every form instance one DefendingArmy record?


Thanks for any suggestions!

Martin

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate