Well, if you can iterate the forms of a formset in the template, you can
do it in python.  I guess the trick is whether there is a public interface
for accessing the model instance of the current form during such an
iteration,  (I'm assuming here that the fields you do want to use a fields
comes from instances of a model, and that the text you want to play
with does too.)  You could iterate (in python) over a zip() of the forms
of the formset and a same order list of the model instance (or something
else that gives you access to the strings (such as a list of the strings).

Of course, if you're creating the formset in a loop already, you could
maybe do it at that time.  I'd guess, however, that you're doing it
with a queryset that's not actually getting iterated until the template
iterates it.  Assuming that these are instances of the same form, you
could override the Form class's __init__ method, which, for model
forms, has access to the instance, could extract the description, and
stick it on an instance of "self", after calling the super class __init__
method.  (Remember to pass instance on to the super clas __init__ if
you capture it with a named argument rather than extracting ti from
kwargs.)  Does that fit?

Bill

On Thu, Sep 2, 2010 at 4:08 PM, bobhaugen <bob.hau...@gmail.com> wrote:
> On Sep 2, 2:54 pm, Bill Freeman <ke1g...@gmail.com> wrote:
>> Let's start by agreeing on common terminology.  "property" has
>>  a meaning in Python slightly different than its usage in some
>> other languages, and I don't think that's what you mean.
>>
>> I'm thinking that what you mean by "property" on the form is what
>> Python calls an attribute of the instance of the form class.
>
> Yeah, sorry about the term. Attribute is what I mean.
>
> [some paragraphs elided for now...]
>
>> And you want to include, somewhere between the statements above:
>>
>>    form.product_description = u'Some string involving "hanging weight"'
>>
>> Am I tracking so far?
>
> Yeah, except I can do that easily for a normal form.  Am trying to
> figure out how to do it for forms in a formset.  I can add the same
> attribute with the same value to all forms in a formset by doing
> something like:
>
> for form in formset.forms:
>    form.project_description = "Beef etc."
>
> But of course each product has a different description.
>
>> If so, in your template, you should, at the appropriate point, be able to
>> say:
>>
>>   <TD>{{ beef_form.product_description }}</TD>
>>
>> That should do what you would think.  The product_description attribute is
>> not used automatically by anything, but is available for the variable 
>> reference
>> in the template.
>>
>> So, is this what you want, and if so have you tried it, and if so what don't
>> you like about the result?
>
> Works great and is less filling for regular forms, and I do it all the
> time.  Including in my set-of-forms which is not a formset, which is
> how I did it before formsets were invented.  I want to do the same
> thing for forms in a formset.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
>
>

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

Reply via email to