On Sep 30, 9:36 pm, SnappyDjangoUser <[EMAIL PROTECTED]> wrote:
> In the example
> above you assume that both p and v belong to products.
I just put them into a combined array called "products"
> In my case,
> that is not the case. I have a queryset of products that contains the
> vendor name, model name and model number. I also have a formset
> containing forms. I am trying to combine these 2 lists of objects
> into the tuple so I can iterate over both in the template.
I don't know how the forms are indexed.
but I would suggest iterating through the product queryset in the
view,
and storing each result in a dict indexed by id. or look at queryset
in_bulk
then when building the tuples you can look up the vendor or product or
whatever it is in the dict.
another trick that I've done recently is to create a quick class
def view(request):
# this class just exists for use in this view
class Combo(object):
def __init__(self,form,product):
self.form = form
self.product = product
then instead of zipping together tuples, you make a list of these
Combos
and pass those in.
{{ combo.form }}
{{ combo.product }}
then I can add methods for things that I will need in that specific
view.
its much simpler than writing a template_tag.
>
> # or more pythonically if its easy to find your vendor:
> zipped = [ (form, << NEED HELP HERE product_queryset>>) for form
> in formset.forms ]
> context = { 'products': zipped }
>
> {% for form, product in products %}
> {{product.vendor}} {{product.model_name}} {{product.model_num}}
> {{form}}
> {% endfor %
>
> Any suggestions?
>
> On Sep 30, 9:45 am, felix <[EMAIL PROTECTED]> wrote:
>
> > malcom is suggesting this:
>
> > def view(request):
> > blah blah blah
> > ...
> > zipped = []
> > for p in products:
> > v = find the vendor for this product
> > zipped.append( ( p, v) ) # add them as a tuple
>
> > # or more pythonically if its easy to find your vendor:
> > zipped = [ (p, vendor for product) for p in products ]
> > context = { 'products': zipped }
>
> > {% for product, vendor in products %}
> > {{product}} {{vendor}}
> > {% endfor %}
>
> > On Sep 30, 5:05 pm, SnappyDjangoUser <[EMAIL PROTECTED]> wrote:
>
> > > Hi Malcolm,
>
> > > You suggested:
>
> > > > set up the data structures
> > > > you pass to your view a bit differently so that you can loop over the
> > > > forms and the products simultaneously (that is, pull apart the formset
> > > > forms and zip them together with the product entries in the view).
>
> > > This is exactly what I like to do! I am still a bit confused on exact
> > > implementation, however, because I have not found a way in Django to
> > > loop through 2 structures at once in a template. The forloop.counter
> > > seems that it is mostly used for printing the iteration through the
> > > loop (not for indexing) and the "for loop" tag in Django is built only
> > > to iterate through one structure at a time. Do you have any examples
> > > how how to loop through 2 strucutres simultaneously?
>
> > > Thanks!
>
> > > -Brian
>
> > > On Sep 30, 12:10 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
> > > wrote:
>
> > > > On Mon, 2008-09-29 at 17:31 -0700, SnappyDjangoUser wrote:
> > > > > Hi Folks,
>
> > > > > How can I use a forloop counter to index into a query set as in the
> > > > > example below?
>
> > > > > (I know this code does not work, but I want to do something of the
> > > > > sort):
>
> > > > > {% for form in quote_product_formset.forms %}
> > > > > <tr>
> > > > > <td>{{ product.(forloop.counter).Vendor }}</td>
> > > > > </tr>
>
> > > > > {{ form }}
> > > > > {% endfor %}
>
> > > > > (in the above case, Vendor is a field in product. So if I want to
> > > > > access the first product when forloop counter is 1, the variable would
> > > > > expland to "{{ product.1.Vendor }}")
>
> > > > You can't do indirect variable references like this in Django's
> > > > templating language. The reasoning is that it ends up complicating the
> > > > template structure when you wander down that path. Instead, factor it
> > > > out into a template tag (that can be passed the current context, so it
> > > > will have access to the forloop counter) or set up the data structures
> > > > you pass to your view a bit differently so that you can loop over the
> > > > forms and the products simultaneously (that is, pull apart the formset
> > > > forms and zip them together with the product entries in the view).
>
> > > > Usually the second approach works a bit more nicely, but either is
> > > > possible.
>
> > > > Regards,
> > > > Malcolm
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---