2011-07-15 148 views
38

嘿,我環視了一下這裏的一些simliar帖子,但沒有找到任何解決了我的問題。我有以下型號,'RelatedManager'對象是不可迭代的Django

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對象,像這樣

{% for area in world_areas %} 

產生的模板語法錯誤

'RelatedManager'對象是不可迭代的

任何人有任何想法,爲什麼這是發生?真的不能似乎圍繞這個! strangley這是在殼爲我工作:我是否缺少明顯的東西?

非常感謝任何人的幫助!

+0

您是否嘗試清除緩存? –

+0

是的,嘗試與緩存! –

+1

所以你確定它沒有從緩存中讀取任何東西? –

回答

8

通常,最好使用valuesvalues_list將數據從查詢集傳遞到模板。

world_areas = Areas.objects.select_related().all().values_list('name', 'order_in_sidebar_network', ...) 

退房的Django docs關於如何使用values功能,如果你以前沒有使用過它的信息。

101

Callall()從管理器中檢索元素。

{% for area in world_areas.all %} 
+4

這是一個很好的解決方案。請標記爲已接受。你也可以在這裏過濾。 –

+3

+1和奇蹟,爲什麼它不被接受爲答案? – andi

+0

,因爲它遲到了2年。 – user1040495

相關問題