0

I get a TypeError when I use the following code in my Django Template:

{% for signature in petition.get_signatures %}
     {% include 'petition/signature.html' with petition=petition %}
{% endfor %}

Here are my models and classes:

class Petition(models.Model):
    title = models.CharField(max_length= 90, default="Enter petition title here")
    created_on = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(null=False, upload_to='static/petition-photos/%Y/%m/%d')
    video = models.CharField(max_length=600, default="Enter an external video link")
    petition = models.TextField(null=False, default="Type your petition here")
    created_by = models.ForeignKey(User)

    def total_likes(self):
        return self.like_set.count()

    def __str__(self):
        return self.title[:50]

    def get_signatures(self):
        return self.signature_set.all

class Signature(models.Model):
    petition= models.ForeignKey(Petition)
    user = models.ForeignKey(User)
    description = models.TextField(null=False, blank=False)
    anonymous = models.BooleanField()
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.petition

I get the following error message when I load my template view page with the 'for' condition. The error message I get is 'method' object is not iterable. What might I be doing wrong? Any solutions? I'm kind of a noob so if you could explain the solution too, that would be great.

1 Answer 1

2

You need to call the .all() method:

def get_signatures(self):
    return self.signature_set.all()

You returned the method object itself, rather than the result it produces when called, and the {% for signature in .. loop tries to iterate over that method object, and can't.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.