Why getting message object not iterable?

3,592 views
Skip to first unread message

pradyumna

unread,
May 16, 2009, 12:07:17 PM5/16/09
to Django users
I am a newbie to Django.. I am wondering why I am getting 'Caught an
exception while rendering: 'Car' object is not iterable?

As per my understanding, in the template, if statement should not be
true and else portion should be executed.

Can anybody help me on this?


View:
--------

def v_list(request,car_number=None):

try:
if car_number!=None:
car_list=Car.objects.get(id=car_number)
else:
car_list=Car.objects.order_by('name')

except:
raise Http404('Requested Info. not found')
cars_render=RequestContext(request,{'car_list':car_list})
return render_to_response('cars.html',cars_render)

Template:
-------------
{% if car_list %}
{% for car in car_list %}
{{car.id}} ,{{car.name}}
{% endfor %}

{%else%}
{{car_list.name}}
{% endif %}

Alex Gaynor

unread,
May 16, 2009, 12:09:58 PM5/16/09
to django...@googlegroups.com
Using get() returns a single object, whereas using others methods returns a QuerySet.  A single object is just that, one object, whereas a QuerySet is a group of objects, so you need to know which one you have to know how to work with it.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Karen Tracey

unread,
May 16, 2009, 12:17:53 PM5/16/09
to django...@googlegroups.com
On Sat, May 16, 2009 at 12:07 PM, pradyumna <joshi.p...@gmail.com> wrote:

I am a newbie to Django.. I am wondering why I am getting 'Caught an
exception while rendering: 'Car' object is not iterable?

As per my understanding,  in the template, if statement should not be
true and else portion should be executed.

Can anybody help me on this?


View:
--------

def v_list(request,car_number=None):

   try:
       if car_number!=None:
           car_list=Car.objects.get(id=car_number)
       else:
           car_list=Car.objects.order_by('name')

Here you have code that in the first part will set car_list to an individual Car instance (get returns a model instance, not a QuerySet) but in the 2nd part will return a QuerySet.  If you want something that is iterable in both cases, you could use fillter() rather than get().
 

   except:
       raise Http404('Requested Info. not found')
    cars_render=RequestContext(request,{'car_list':car_list})
   return render_to_response('cars.html',cars_render)

Template:
-------------
  {% if car_list %}
           {% for car in car_list %}
                {{car.id}} ,{{car.name}}
           {% endfor %}

   {%else%}
           {{car_list.name}}
   {% endif %}

This if/else construct doesn't make much sense.  In the else case (when there is no car_list), you are still using car_list and attempting to access its 'name'.  So perhaps you do not want to be using filter() above but rather want to be setting a different variable (car_instance?) on the return from get() and passing both car_list and car_instance the template, where you can handle displaying different results depending on which variable is not None.

Karen
Reply all
Reply to author
Forward
0 new messages