I think I did my due google diligence (googlence?) on this one, and came up 
short.

I'm trying to pass some ancillary, read-only information along with each 
element in a formset. Ie, Along with the stuff that can be changed by the user, 
I'd like some stuff that can-NOT be changed. Not all, but some, of the 
information needs to be read back during validation.

For example, I have:

class Thingy ( models.Model ):
        name = model.CharField ()


I have a few that selects a few Thingys and will present them to the user with 
a checkbox next to each, so its obvious I want a form:

class CheckThingyForm ( forms.Form ):
        checked = forms.BooleanForm ()

CheckThingyFormset = formset_factory ( CheckThingyForm, extra=0 )


Inside the view, I create a list of the thingies I want to present

        thingies = Thingy.objects.filter ( ... some criteria... )

and now to... somehow... create the initial data for the formset. This is where 
I am stumped. I've tried something like the following:

        thingies_list = []
        for thingy in thingies:
                thingies_list.append ( { 'thingy': thingy } )

        formset = CheckThingyFormset ( initial = thingies_list )

And while this presents the correct number of rows, my expectation on being 
able to extract the thingy in the template fails. "form.thingy.name" returns 
nothing

{% for form in formset %}
<tr><td>{{ form.checked }}</td><td>{{ form.thingy.name }}</td></tr>
{% endfor %}

If I'm far more explicit about it, by using the following, i still turn up a 
blank

thingies_list.append ( { 'name': thingy.name } )


What's the "right" way of passing ancillary data to a formset like this? If it 
was just a single form, then I could very easily just pass the data i needed 
through additional context variables. But since here I want to pass some data 
per form, and dont care about being able to read it back or not, it makes sense 
to try and pass it along WITH the form, and not need it to be a field itself to 
read back.

(What I omitted was needing to pass thingy.id, so I know what thingy I'm 
actually checking when validating the form... obviously I should include that 
in the form or an "add_fields in the formset, and have it as a hidden... I 
could do the same thing with the "name" and just extract the value explicitly, 
but that's really contorted)


Thanks for reading this far... I hope someone can help.


-- 
  ("`-/")_.-'"``-._        Chris Cogdon <ch...@cogdon.org>
   . . `; -._    )-;-,_`)
  (v_,)'  _  )`-.\  ``-'
 _.- _..-_/ / ((.'
((,.-'   ((,/   fL

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

Reply via email to