'RelatedManager' 对象不是可迭代的 Django [英] 'RelatedManager' object is not iterable Django

查看:31
本文介绍了'RelatedManager' 对象不是可迭代的 Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我在 SO 上浏览了一些类似的帖子,但没有找到任何解决我问题的方法.我有以下型号,

Hey i have looked around through some simliar posts here on SO but havent found anything that has solved my problem. I have the following models,

from django.db import models

class Areas(models.Model):

    name =  models.CharField(max_length = 120)
    order_in_sidebar_network = models.IntegerField(blank=True, null=True)
    order_in_section_network = models.IntegerField(blank=True, null=True)


    def __unicode__ (self):
        return self.area_name

    class Meta:
        verbose_name_plural = "Areas"
        verbose_name = "Area"

class Countries(models.Model):
    name = models.CharField(max_length = 120, help_text = "The name of the country")
    area = models.ForeignKey(Areas, verbose_name = 'Area')

    def __unicode__ (self):
        return self.name

    class Meta:
        verbose_name_plural = "Countries"
        verbose_name = "Country"
        ordering = ['name']



class Offices(models.Model):
    country = models.ForeignKey(Countries, verbose_name = 'Country')
    name = models.CharField(max_length = 255, help_text = "The name of this office, IE London")
    main_office = models.BooleanField(default= False, help_text = "Is this office a key location?", verbose_name = "Key Location")
    address_1 = models.CharField(max_length = 255, null = True, blank = True)
    address_2 = models.CharField(max_length = 255, null = True, blank = True)
    address_3 = models.CharField(max_length = 255, null = True, blank = True)
    city = models.CharField(max_length = 255, null = True, blank = True)
    postcode = models.CharField(max_length = 20)
    tel = models.CharField(max_length = 30, null= True, blank = True, help_text = "Optional telephone contact number")
    mobile = models.CharField(max_length = 30, null= True, blank = True, help_text = "Optional mobile contact number")
    fax = models.CharField(max_length = 30, null= True, blank = True, help_text = "Optional fax contact number")
    data_1 = models.CharField(max_length = 255, null = True, blank = True, help_text = "Optional additional data", verbose_name = "Additional information")
    data_2 = models.CharField(max_length = 255, null = True, blank = True, help_text = "Optional additional data", verbose_name = "Additional information")

    class Meta:
        verbose_name_plural = "Offices"
        verbose_name = "Office"
        ordering = ['name']

    def __unicode__(self):
        return self.name

class OfficeMembers(models.Model):
    name = models.CharField(max_length = 60, help_text = "Please tell us this person name")
    title = models.CharField(max_length = 100, help_text = "The person's title, IE Managing Director")
    email = models.EmailField(max_length = 255, null = True, blank = True, help_text = "Optional email address for this person")
    email2 = models.EmailField(max_length = 255, null = True, blank = True, help_text = "Optional second email address for this person")
    phone = models.CharField(max_length = 30, null = True, blank  = True, help_text = "Optional contact number for this person")
    mobile = models.CharField(max_length = 30, null = True, blank  = True, help_text = "Optional mobile contact number for this person")
    office = models.ForeignKey(Offices, null = True)
    class Meta:
        verbose_name_plural = "Office Memebers"
        verbose_name = "Office memebr"
        ordering = ['name']

    def __unicode__(self):
        return self.name

我设置了以下视图

def index(request):

    cache_key = "world_areas"
    cache_time = 60

    world_areas_cache = cache.get(cache_key)

    #if no cache is set, grab the objects, and set the cache

    logger.debug(world_areas)
    if not world_areas_cache:
        logger.info('No cache found grabbing objects')
        world_areas = Areas.objects.select_related().all()
        #cache.set(cache_key, world_areas, cache_time)
        logger.debug(world_areas)
    else:
        logger.info("Getting from cache")
        world_areas = world_areas_cache

    return render_to_response('network/index.html', {'world_areas':world_areas}, context_instance=RequestContext(request))

尝试像这样遍历 world_areas 对象

trying to iterate over the world_areas object like so

{% for area in world_areas %}

产生模板语法错误

'RelatedManager' 对象不可迭代

'RelatedManager' object is not iterable

有人知道为什么会这样吗?似乎真的无法绕过这个!扼杀这在 shell 中对我有用:我错过了一些明显的东西吗???

Any one got any ideas why this is happeing? really cant seem to get round this! strangley this is working for me in shell :S am i missing something obvious???

非常感谢任何能够提供帮助的人!

Big thanks to anyone able to help!

推荐答案

调用 all() 从管理器中检索元素.

Call all() to retrieve the elements from the manager.

{% for area in world_areas.all %}

这篇关于'RelatedManager' 对象不是可迭代的 Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆