根据所选类别过滤/显示元素

时间:2019-02-24 12:52:40

标签: django django-views

我对Django还是一个新手,我坚持了一个Issue。我已经建立了如下定义的模型:

# models.py

from django.db import models
from bifrost.models import CustomUser
from django.urls import reverse

# Create your models here.


# Model Projektu
class Project(models.Model):
    PROJECT_TYPE = (
        ('Scrum', 'Scrum'),
        ('Kanban', 'Kanban'),
    )
    project_key = models.CharField(max_length=8)
    project_name = models.CharField(max_length=160)
    project_type = models.CharField(max_length=10, choices=PROJECT_TYPE)
    project_lead = models.ForeignKey(CustomUser, on_delete=models.CASCADE, null=True, blank=True)
    project_date_created = models.DateField(null=True, blank=True)


    def __str__(self):
        return self.project_name

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

class Issue(models.Model):
    ISSUE_PRIORITY = (
        ('C', 'Critical'),
        ('H', 'High'),
        ('M', 'Medium'),
        ('L', 'Low'),
    )
    issue_type = models.TextField(default='Issue', editable=False)
    issue_id = models.IntegerField(primary_key=True)
    issue_date_created = models.DateField()
    issue_date_updated = models.DateField(null=True, blank=True)
    issue_project = models.ForeignKey(Project, on_delete=models.CASCADE)
    issue_title = models.TextField()
    issue_description = models.TextField(null=True, blank=True)
    issue_epic = models.ForeignKey(Epic, null=True, blank=True, on_delete=models.CASCADE)
    issue_sprint = models.ForeignKey(Sprint, on_delete=models.CASCADE, null=True, blank=True)
    issue_priority = models.CharField(max_length=8, choices=ISSUE_PRIORITY)
    issue_assignee = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='assignees', null=True, blank=True) 
    issue_author = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='authors') 
    issue_remaining_estimate = models.IntegerField(null=True, blank=True) # w minutach
    issue_time_logged = models.IntegerField(default='0') 
    issue_attachment = models.FileField(null=True, blank=True)
    issue_backlog_order = models.IntegerField(null=True, blank=True)

现在,我想显示与所选项目相关的问题列表。我读过有关在视图或正确的查询集中创建字典的信息,但是没有一种解决方案对我有用:(我如何动态地动态选择用户选择的上下文(项目)并基于该过滤器问题?

views.py

from django.shortcuts import get_object_or_404
from django.views.generic import ListView, DetailView
from .models import Project, Issue, Epic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic import TemplateView
from django.urls import reverse_lazy


# Create your views here.

class ProjectListView(ListView):
  model = Project
  template_name = 'project-list.html'
  context_object_name = 'projects_list'

class ProjectBacklogView(DetailView):
  template_name = 'project-backlog.html'
  context_object_name = 'backlog'

urls.py

from django.urls import path
from .views import ProjectListView, ProjectDetailView, ProjectCreateView, ProjectUpdateView, ProjectDeleteView, ProjectBacklogView

urlpatterns = [
  path('<int:pk>/delete', ProjectDeleteView.as_view(), name='project-delete'),
  path('<int:pk>/edit/', ProjectUpdateView.as_view(), name='project-edit'),
  path('new/', ProjectCreateView.as_view(), name='project-create'),
  path('<int:pk>/', ProjectDetailView.as_view(), name='project-detail'),
  path('<int:pk>/backlog', ProjectBacklogView.as_view(), name='project-backlog'),
  path('', ProjectListView.as_view(), name='project'),
 ]

在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

  

现在,我想显示与所选项目相关的问题列表。

您可以简单地将该逻辑放入积压模板(project-backlog.html)中。您可以使用backlog.issue_set.all()访问包含相关问题的查询集,因此可以在模板中使用以下方式呈现相关问题:

<!-- project-backlog.html -->
{% for issue in backlog.issue_set.all %}
{{ issue.name }}
{% endfor %}

因此,在这里,我们遍历与Issue对象相关的backlog,并渲染issue.name。当然,我们可以使其更复杂,并将其呈现为表格。例如:

<!-- project-backlog.html -->
<table>
<tr><th>Name</th></tr>
{% for issue in backlog.issue_set.all %}
<tr><td>{{ issue.name }}</td></tr>
{% endfor %}
</table>

以上内容当然仍然是草稿,可能需要进行一些编辑才能产生更具视觉吸引力的内容。