Django:'long'对象不可迭代

时间:2012-07-08 12:57:53

标签: python django

  1. 我有一个模特

    class tournaments(models.Model):
        # ....
        total_rounds = models.IntegerField(max_length=11, blank=True, null=True)
        # ....
    
        def get_total_rounds_count(self):
            return self.total_rounds
    
  2. views.py:

    def tourney_view(request, offset):
        # .....
        if (offset):
            selected_tournament =  tournaments.objects.get(id=offset)
        return render_to_response('tournament.html', {'tournament': selected_tournament})
    
  3. 在'tournament.html'模板中,我试图遍历total_rounds:

    {% for q in tournament.get_total_rounds_count%}
    {% endfor %}
    
  4. 得到错误:'long'对象不可迭代 为什么?我的字段是IntegerField,我只是试图遍历整数值,但得到“不可迭代”

3 个答案:

答案 0 :(得分:3)

您可以使用此代码段:http://djangosnippets.org/snippets/1357/

或定义返回Tournament.get_total_rounds

range(get_total_rounds_count)

答案 1 :(得分:2)

是的,奥托的方式是最好的,只需更新你的功能即可返回范围。

 class tournaments(models.Model):
     # ....
     total_rounds = models.IntegerField(max_length=11, blank=True, null=True)
     # ....

     def get_total_rounds_count(self):
         return range(self.total_rounds)

答案 2 :(得分:0)

那是因为你不能使用for构造数字