passing more information with formsets

2 views
Skip to first unread message

Alastair Campbell

unread,
Mar 14, 2010, 8:14:12 PM3/14/10
to Django users
Hi,

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

Dennis Kaarsemaker

unread,
Mar 16, 2010, 3:46:22 PM3/16/10
to django...@googlegroups.com
On ma, 2010-03-15 at 00:14 +0000, Alastair Campbell wrote:

> 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.

Alastair Campbell

unread,
Mar 17, 2010, 5:34:46 AM3/17/10
to django...@googlegroups.com
On Tue, Mar 16, 2010 at 7:46 PM, Dennis Kaarsemaker wrote:
> Each ModelForm has an instance attribute, so you can use
> {{ form.instance.whatever }}

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

Dennis Kaarsemaker

unread,
Mar 17, 2010, 3:02:04 PM3/17/10
to django...@googlegroups.com
On wo, 2010-03-17 at 09:34 +0000, Alastair Campbell wrote:
> On Tue, Mar 16, 2010 at 7:46 PM, Dennis Kaarsemaker wrote:
> > Each ModelForm has an instance attribute, so you can use
> > {{ form.instance.whatever }}
>
> 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?

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.

Alastair Campbell

unread,
Mar 17, 2010, 8:34:42 PM3/17/10
to django...@googlegroups.com
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.)

>> 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

hcarvalhoalves

unread,
Mar 18, 2010, 11:48:30 AM3/18/10
to Django users

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 %}

Reply all
Reply to author
Forward
0 new messages