for循环中每个类别的元素列表

时间:2019-03-09 21:15:23

标签: django django-templates django-views

我有一种情况,我想在模板视图上显示所有对象(问题)和类别(冲刺)的列表。我已经应用了另一个过滤器来显示属于特定项目的问题。

到目前为止,我已经设法显示Sprint,并且在每个Sprint中我都显示问题列表。我该如何对问题应用其他过滤,仅显示属于特定冲刺的问题?

最终结果应该是显示每个冲刺划分的所有问题的列表。

我的下面的代码:

    #views.py
    class ProjectBacklogView(DetailView):
        model = Project
        template_name = 'project-backlog.html'
        context_object_name = 'backlog'

        def get_context_data(self, **kwargs):
            context = super(ProjectBacklogView, self).get_context_data(**kwargs)
            context['project'] = Project.objects.all()
            context['issue'] = Issue.objects.all().order_by('sprint')
            # context['epic'] = Epic.objects.all()
            # context['initiative'] = Initiative.objects.all()
            context['sprint'] = Sprint.objects.all()
            return context

    # template.html
    {% extends 'base-project.html' %}

    {% block content %}
    {% for sprint in backlog.sprint_set.all %}
    <h4>{{ sprint.name }}</h4>
            <div id="simpleList" class="list-group" style="margin-bottom: 2%;">
                    {% for issue in backlog.issue_set.all %}
                    <div class="list-group-item">
                            <div style="float:left;"><strong><a href="{% url 'issue-edit' issue.pk %}">{{ issue.title }}</a></strong></div>
                            <div style="float: right;"><a href="#" class="btn btn-success btn-circle btn-sm"><i class="fas">{{ issue.remaining_estimate }}</i></a></div>
                            <br /><br /><hr style="border-top: dashed 1px;"></hr>
                                <div style="float: left;"><small>Assignee: <strong>{{ issue.assignee }}</strong></small></div>
                                <div style="float: right;"><small>Initiative: <strong>{{ issue.initiative }}</strong></small></div><br />
                                <div style="float: left;"><small>Priority: <strong>{{ issue.priority }}</strong></small></div>
                                <div style="float: right;"><small>Epic: <strong>{{ issue.epic }}</strong></small></div>

                    </div>
                    {% endfor %}
                    <br />
            </div>
    {% endfor %}
    {% endblock %}

#models.py
class Project(models.Model):
    PROJECT_TYPE = (
        ('SCR', 'Scrum'),
        ('KAN', 'Kanban'),
    )
    # slug = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    alias = models.CharField(max_length=8, primary_key=True)
    name = models.CharField(max_length=160)
    project_type = models.CharField(max_length=10, choices=PROJECT_TYPE, default="SCR")
    lead = models.ForeignKey(CustomUser, on_delete=models.CASCADE, null=True, blank=True)
    date_created = models.DateTimeField(auto_now_add=True)

# Definicja nazwy modelu w Adminie Django
    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('project-detail', args=[str(self.alias)])

class Sprint(models.Model):
    # sprint_type = models.TextField(default='Sprint', editable=False)
    name = models.CharField(max_length=32)
    goal = models.TextField(null=True, blank=True)
    start_date = models.DateField()
    end_date = models.DateField()
    project = models.ForeignKey(Project, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

class Issue(models.Model):
    ISSUE_PRIORITY = (
        ('Critical', 'C'),
        ('High', 'H'),
        ('Medium', 'M'),
        ('Low', 'L'),
    )
    issue_type = models.TextField(default='Issue', editable=False)
    issue_id = models.AutoField(primary_key=True)
    # slug = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    title = models.CharField(max_length=128)
    description = models.TextField(null=True, blank=True)
    initiative = models.ForeignKey(Initiative, on_delete=models.CASCADE, null=True, blank=True)
    epic = models.ForeignKey(Epic, null=True, blank=True, on_delete=models.CASCADE)
    sprint = models.ForeignKey(Sprint, on_delete=models.CASCADE, null=True, blank=True)
    priority = models.CharField(max_length=8, choices=ISSUE_PRIORITY, default='Medium')
    assignee = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='assignees', null=True, blank=True) # zczytywane z tabeli userów
    author = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='authors') # zczytywane z tabeli userów
    remaining_estimate = models.IntegerField(null=True, blank=True, default='0') # w minutach
    time_logged = models.IntegerField(default='0') # w minutach
    attachment = models.FileField(null=True, blank=True) # To Do - multiple files??

    def __str__(self):
       return self.title

    def get_absolute_url(self):
        return reverse('issue-detail', args=[str(self.issue_id)])

我的问题是,for loop中列出了冲刺,因此我不知道如何动态地应用过滤器(基于字段冲刺)

0 个答案:

没有答案