2011-07-15 38 views
38

Hey tôi đã xem xét xung quanh thông qua một số bài viết simliar ở đây trên SO nhưng havent tìm thấy bất cứ điều gì đã giải quyết vấn đề của tôi. Tôi có các mô hình sau,đối tượng 'RelatedManager' không thể lặp lại 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 

tôi đã quan điểm sau đây thiết lập

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)) 

cố gắng để lặp qua các world_areas đối tượng như vậy

{% for area in world_areas %} 

tạo ra một lỗi mẫu cú pháp

đối tượng 'Trình quản lý liên quan' không thể lặp lại

Bất kỳ ai có bất kỳ ý tưởng nào về điều này đang xảy ra? thực sự không thể có vẻ để có được vòng này! strangley này là làm việc cho tôi trong vỏ: S am i thiếu một cái gì đó rõ ràng?

Cảm ơn bất kỳ ai có thể trợ giúp!

+0

Bạn đã thử xóa bộ nhớ cache chưa? –

+0

Đã thử với bộ nhớ cache! –

+1

Vì vậy, bạn chắc chắn rằng nó không đọc bất cứ điều gì từ bộ nhớ cache? –

Trả lời

8

Nói chung, thực hành tốt hơn là sử dụng values hoặc values_list để chuyển dữ liệu từ bộ truy vấn đến mẫu.

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

Kiểm tra các Django docs để biết về cách sử dụng các chức năng values nếu bạn chưa sử dụng nó trước đây.

101

Callall() để truy xuất các yếu tố từ người quản lý.

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

đây là một giải pháp tốt. xin vui lòng đánh dấu là chấp nhận. bạn cũng có thể lọc ở đây. –

+3

+1 và tự hỏi, tại sao nó không được chấp nhận như một câu trả lời? – andi

+0

vì nó đã được trả lời 2 năm muộn. – user1040495