基于选定类别的过滤/显示元素



我仍然是django的新手,我遇到了一个问题。我已经构建了以下定义的模型:

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

现在,我想显示与选定项目有关的问题列表。我阅读了有关在视图或适当的QuerySet中创建字典的信息,但是没有解决方案对我有用:(如何动态地选择哪个上下文(项目)用户并根据该过滤器进行正确的问题?

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'),
 ]

非常感谢您的帮助!

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

您可以简单地将该逻辑放入积压的模板中(project-backlog.html)。您可以访问包含backlog.issue_set.all()相关问题的QuerySet,因此在模板中可以呈现相关问题,例如:

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

在这里,我们在与backlog对象相关的Issue s上迭代,然后渲染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>

以上当然仍然是草稿,它可能需要一些编辑才能产生更具吸引力的东西。

最新更新