Ali khan
Ali khan

Reputation: 585

Django model is not iterable in template

I am trying to iterate through a model to get first image in the list and it is giving me error that model is not iterable. Following is my code for the model and template. I just need to get the first image in list associated with single product.

models.py:

class Product(models.Model):
    title = models.CharField(max_length=500)
    description = models.TextField(blank=True, null=True)
    price = models.DecimalField(max_digits=20, decimal_places=2)
    sku = models.CharField(null=True, max_length=100)
    url = models.URLField(blank=True)
    slug = models.SlugField(unique=True)
    categories = models.ManyToManyField('Category', blank=True)
    default = models.ForeignKey('Category', related_name='default_category', null=True, blank=True)
    provider = models.ForeignKey('Product', on_delete=models.CASCADE)

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('product_detail', kwargs={"slug": self.slug})

    def get_image_url(self):
        img = self.productimage_set.first()
        if img:
            return img.image.url
        return img
    def get_image_list(self):
        return self.productimage_set.all()

def image_upload_to(instance, filename):
    title = instance.product.title
    slug = slugify(title)
    basename, file_extension = filename.rsplit('.', 1)
    new_filename = '%s-%s.%s' % (basename, instance.id, file_extension)
    return 'products/%s/%s' % (slug, new_filename)

class ProductImage(models.Model):
    product = models.ForeignKey(Product)
    image = models.ImageField(upload_to=image_upload_to)

    def __unicode__(self):
        return self.product.title

prduct_detail.html:

<div class="pics clearfix">
<div class="thumbs">
{% for img in product.get_image_list %}
<div class="preview">
<a href="{{ img.get_absolute_url }}" class="selected" data-full="{{ img.image.url }}" data-title="title"> <img src="{{ img.image.url }}"> </a>
</div>
{% endfor %}
</div>
<div class="imglarge">
{% for img in object.productimage_set.all %}

<a href="{{ object.get_absolute_url }}" class="full" title="title"> <img src="{{ img.image.url }}"> </a>

{% endfor %}

</div>
</div>

Image list is coming fine but first image is not coming up as either it shows all the image with {% for img in object.productimage_set.first %}it shows error below:

'ProductImage' object is not iterable
Request Method: GET
Request URL:    http://localhost:8000/items-for-kids
Django Version: 1.8.13
Exception Type: TypeError
Exception Value:    
'ProductImage' object is not iterable
Exception Location: C:\Users\AliKhan\eCom\lib\site-packages\django\template\defaulttags.py in render, line 161
Python Executable:  C:\Users\AliKhan\eCom\Scripts\python.EXE
Python Version: 2.7.9

Upvotes: 4

Views: 2603

Answers (2)

ndpu
ndpu

Reputation: 22571

You can use with tag instead of for to avoid error:

Change this:

{% for img in object.productimage_set.first %}
...
{% endfor %}

to

{% with img=object.productimage_set.first %}
...
{% endwith %}

Upvotes: 2

Moses Koledoye
Moses Koledoye

Reputation: 78554

You use all to get all the related product images and iterate through them:

{% for img in object.productimage_set.all %}

But if you get a single object, like when you get the first related product image then you can't iterate:

{{ object.productimage_set.first }}

That's why you get the error

object is not iterable

You only have a single object. You should consider using all if you need all the objects.

Upvotes: 4

Related Questions