I have a formset that is working well, but I'd like to use other
information within that loop:
{% for form in formset.forms %}
<fieldset>
<legend>[Member name would go here]</legend>
<table class="form">
{{ form }}
</table>
</fieldset>
{% endfor %}
There is a queryset of member objects from the same view that matches
the forms in the formset, but I assume I can't do a loop within the
loop?
Is there a way to loop through the members at the same time? Or some
other way of accessing the matching objects?
Hopefully it's probably a simple python thing, but I'm fairly new to
python/django.
Kind regards,
-Alastair
> Is there a way to loop through the members at the same time? Or some
> other way of accessing the matching objects?
Each ModelForm has an instance attribute, so you can use
{{ form.instance.whatever }}
--
Dennis K.
The universe tends towards maximum irony. Don't push it.
Hi Dennis,
Unfortunately it is not a model form. The form creates an "entry",
which is de-normalised data taken from the person (member) details and
the event details.
The closest I've gotten so far is to use something like this in the
formset loop:
{% for member in members %}
{% ifequal member.sail_number form.sail_number.data %}
{{ member.name}}
{% endifequal %}
{% endfor %}
Unfortunately not all members have sail numbers, so I'll try passing
initial data for member.id through to the form as a better match.
Unless there's a better way to do it?
Also, is there a danger to using form.something.data? It isn't in the
docs, but I found it as a possible way of matching the form against
the object values.
Kind regards,
-Alastair
In the view function, you could add an attribute (say, instance :-)) to
each form in the formset. That to me is a better way than doing weird
magic in a template.
> Also, is there a danger to using form.something.data? It isn't in the
> docs, but I found it as a possible way of matching the form against
> the object values.
Maybe you can run into user input creepiness, such as someone overriding
the field in the post request and being able to see data from other
objects. Not sure how realistic that is.
You're probably right, but I couldn't work that out for formsets:
http://dpaste.com/172317/
Is there a standard programming pattern for matching up two loops?
(I'm not a programmer by trade.)
>> Also, is there a danger to using form.something.data? It isn't in the
>> docs, but I found it as a possible way of matching the form against
>> the object values.
I've put in a hidden field for that (for now), it's only used for
display, it isn't used by any subsequent view, so hopefully doesn't
cause any problems.
Thanks,
-Alastair
On Mar 17, 9:34 pm, Alastair Campbell <ala...@gmail.com> wrote:
> On Wed, Mar 17, 2010 at 7:02 PM, Dennis Kaarsemaker
>
> <den...@kaarsemaker.net> wrote:
> > In the view function, you could add an attribute (say, instance :-)) to
> > each form in the formset. That to me is a better way than doing weird
> > magic in a template.
>
> You're probably right, but I couldn't work that out for formsets:http://dpaste.com/172317/
>
> Is there a standard programming pattern for matching up two loops?
> (I'm not a programmer by trade.)
>
Yes. Return pairs like [{'form': form1, 'instance': instance1}, ...]
and iterate on the template.
{% for pair in items %}
<h1>{{ pair.instance.whatever }}</h1>
<table>{{ pair.form }}</table>
{% endfor %}