2011-07-03 9 views
1

我已經定義了以下型號:Django的{%重整旗鼓%}產生重複的組

class Topic(models.Model): 
class Meta: 
    ordering = [ 
     'title'] 

objects = models.Manager() 
highlighted = HighlightedTopicManager() 

highlight = models.BooleanField(
    default=False, 
    help_text='Show this topic on the home page?', 
    db_index=True) 
highlight_order = models.PositiveSmallIntegerField(
    default=0, 
    help_text='In what order do you want this to be added on the home page?'\ 
     ' Leave blank for alphabetic order.', 
    db_index=True) 
title = models.CharField(
    max_length=2048, 
    db_index=True) 
slug = models.SlugField(
    max_length=128, 
    db_index=True) 
excerpt = models.TextField(
    null=True, 
    blank=True) 
description = models.TextField() 

def _get_content(self): 
    if self.excerpt: 
     return self.excerpt 
    return self.description 
content = property(_get_content) 

@models.permalink 
def get_absolute_url(self): 
    return ('academic_projects_topic_detail',(), {'slug': self.slug}) 

def __unicode__(self): 
    return self.title 

class Project(models.Model): 
class Meta: 
    ordering = [ 
     'topic', 
     'modified', 
     'created'] 

objects = models.Manager() 
highlighted = HighlightedProjectManager() 

highlight = models.BooleanField(
    help_text='Highlight this in the projects\' main page?'\ 
     ' Only the most recently modified one will be displayed.') 
redirect_to = models.URLField(
    blank=True, 
    null=True, 
    help_text='Use this for old or extenal projects.') 
short_title = models.CharField(
    max_length=1024, 
    db_index=True) 
slug = models.SlugField(
    max_length=128, 
    db_index=True) 
title = models.CharField(
    max_length=2048, 
    db_index=True) 
created = models.DateTimeField(
    auto_now_add=True) 
modified = models.DateTimeField(
    auto_now=True) 
excerpt = models.CharField(
    max_length=1024, 
    null=True, 
    blank=True, 
    help_text='Concise description to show in the listing page.') 
description = models.TextField(
    null=True, 
    blank=True, 
    help_text='This content will be rendered right after the title.') 
downloads = models.ManyToManyField(
    Download, 
    null=True, 
    blank=True, 
    help_text='Downloadable files') 
footer = models.TextField(
    null=True, 
    blank=True, 
    help_text='This content will be rendered at the bottom of the page.') 
people = models.ManyToManyField(
    Person, 
    help_text='People involved in this project.', 
    related_name='projects') 
organizations = models.ManyToManyField(
    Organization, 
    help_text='Organizations involved other than the lab.', 
    blank=True, 
    null=True, 
    related_name='projects') 
publications = models.ManyToManyField(
    Publication, 
    blank=True, 
    null=True) 
topic = models.ForeignKey(
    Topic, 
    verbose_name=_('Main topic'), 
    help_text='This is the main topic.', 
    related_name='projects') 
sponsors = models.ManyToManyField(
    Sponsor, 
    blank=True, 
    null=True, 
    help_text='sponsored_projects') 
related_topics = models.ManyToManyField(
    Topic, 
    null=True, 
    blank=True, 
    help_text='Optional related topics.', 
    related_name='secondary_projects') 

def __unicode__(self): 
    return self.short_title 

@models.permalink 
def get_absolute_url(self): 
    return ('academic_projects_project_detail',(), {'slug': self.slug}) 

,並使用以下模板來製作這個網頁(http://seclab.cs.ucsb.edu/academic /項目/):

{% extends "academic/project_base.html" %} 

{% block content_title %}Projects{% endblock %} 
{% block title %}Projects - {{block.super}}{% endblock %} 

{% block content %} 
    {% regroup object_list|dictsort:"topic" by topic as topic_list %} 

    {% for topic in topic_list %} 
    <h2 id="{{ topic.grouper.slug }}">{{ topic.grouper }} <a href="#{{ topic.grouper.slug }}">#</a></h2> 
    {% for project in topic.list %} 
    <h3><a href="{{ project.get_absolute_url }}">{{ project }}</a></h3> 
    <p>{{ project.title }}</p> 
    {% endfor %} 
    {% endfor %} 
{% endblock %} 

這背後的視圖是一個通用的一個,並且援引爲:

url(r'^$', 
    cache_page(ListView.as_view(
      queryset=Project.objects.order_by('topic'), 
      template_name='academic/project_list.html')), 
    name='academic_projects_project_list'), 

所以,Project小號AR e已按排序Topic排序。不幸的是,這段代碼會導致重複gorups,並且有時候,每次刷新時組都會改變(或者至少當我重新啓動服務器時它們會改變)。

任何想法,爲什麼會發生這種情況?除了模板之外,整個代碼位於此處:https://bitbucket.org/phretor/django-academic/src/

+1

而您的輸出是? –

回答

1

dictsort過濾器僅適用於dicts列表,您在此不需要它。您的模板應爲

{% regroup object_list by topic as topic_list %} 
+0

謝謝。這解決了這個問題。 – phretor