显示所有相关条目django

时间:2014-03-04 23:23:44

标签: django django-templates django-views

我有以下型号:

项目模型:

class Project(models.Model):
    user = models.ForeignKey(User, null=True, blank=True)
    title = models.CharField(max_length=180)
    description = models.TextField(max_length=1000, null=True, blank=True)
    slug = models.SlugField()
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    active = models.BooleanField(default=True)

    IN_PROGRESS = 'Manuscript in Progress'
    COLLABORATORS_NEEDED = 'Looking for Collaborators'
    REVIEW = 'Manuscript Under Revision'
    SUBMITTED = 'Manuscript Submitted'

    CURRENT_STATUS_CHOICES = ((IN_PROGRESS, 'Manuscript In Progress'),     (COLLABORATORS_NEEDED, 'Looking for Collaborators'), (REVIEW, 'Manuscript Under Revision'), (SUBMITTED, 'Manuscript Submitted'))
    current_status  = models.CharField(max_length=50, choices = CURRENT_STATUS_CHOICES, default=IN_PROGRESS)
    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ['-title', '-current_status', 'active', '-updated']

和与Project模型关联的作业(HelpWanted)模型:

class HelpWanted(models.Model):
    project = models.ForeignKey(Project)

    STATISTICIAN = 'Statistical or Quantitative'
    DATA_ENTRY = 'Data Entry'
    RESEARCH = 'Literature Review'
    OTHER = 'Other'

    POSITION_CHOICES = ((STATISTICIAN, 'Statistical or Quantitative'), (DATA_ENTRY,'Data Entry'), (RESEARCH, 'Literature Review'), (OTHER,'Other'))
    Position = models.CharField(max_length=50, choices=POSITION_CHOICES, null=True, blank=True)
    description = models.TextField(max_length=250, null=True, blank=True)
    start_date = models.DateField(null = True, auto_now = False, blank=True)
    end_date = models.DateField(null = True, auto_now = False, blank=True)
    time_commitment = models.IntegerField(null=True, blank=True)
    # slug = models.SlugField()
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)

    def __unicode__(self):
        return str(self.Position)

    class Meta:
        verbose_name=u'Available Position'
        verbose_name_plural = 'Available Positions'

我希望有一个all_jobs列表页面,但希望每个项目都有一个标题,后跟关联的作业。

即。如果我有一个名为“冰淇淋摊”的项目并且有3个工作:

  1. 买家
  2. 设备
  3. 卖方
  4. 我希望能够显示:

    冰淇淋摊位

    1. 买家
    2. 设备
    3. 卖方
    4. 如果我应该在模板中使用for循环,我似乎无法使for循环正确。

      谢谢!

2 个答案:

答案 0 :(得分:1)

假设您的模板被赋予了一个名为projects的变量,它是所有项目的查询集,您可以这样做:

{% for project in projects %}
  <h1>{{ project.title }}</h1>
  <ol>
    {% for help in project.helpwanted_set.all %}
      <li>{{ help.Position }}</li>
    {% endfor %}
  </ol>
{% endfor %}

您还可以传入名为positions的变量,即所有可用位置的查询集,并使用regroup模板标记显示它们:

{% regroup positions by project as positions_list %}
{% for project in positions_list %}
  <h1>{% project.grouper %}</h1>
  <ol>
    {% for position in project.list %}
      <li>{{ position.Position }}</li>
    {% endfor %}
  </ol>
{% endfor %}

答案 1 :(得分:0)

首先,您需要将HelpWanted模型中使用的外键命名为:

project = models.ForeignKey(Project, related_name='jobs')

之后,您可以在视图中使用以下命令在一个查询中加载包含与其相关的所有作业的项目:

Project.objects.(your filter).select_related().prefetch_related('jobs')

在您的模板中使用

{{ project }}
<ul>
{% for job in project.jobs.all %}
    <li>{{ job }}</li>
{% endfor %}
</ul>

希望能帮到你;)