我正在使用清单应用程序,每个清单都有带有类别的任务。
我想按以下格式列出每个类别的任务:
Category 1
Task 1 of Category 1
Task 2 of Category 1
Category 2
Task 1 of Category 2
Task 2 of Category 2
我已经尝试过此答案-Django: List Products of each Categories in a page
但是总是有一个空白列表,没有任何错误。
class Checklist(models.Model):
slug = models.SlugField(max_length=250, unique=True, blank=True)
name = models.CharField(max_length=250, unique=True)
profile = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
category = models.ForeignKey(
ChecklistCategory,
blank=True,
null=True,
on_delete=models.SET_NULL
)
language = models.ForeignKey(
ChecklistLanguage,
blank=True,
null=True,
on_delete=models.SET_NULL
)
image = models.ImageField(
upload_to='images/checklist/cover/%Y/%m/%d/',
default='default_cover.png',
blank=True,
null=True
)
likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='checklist_likes', blank=True)
class Meta:
ordering = ['-name']
def get_like_url(self):
return reverse('checklist_like_toggle_url', kwargs={'slug': self.slug})
def get_absolute_url(self):
return reverse('checklist_url', kwargs={'slug': self.slug})
def save(self, *args, **kwargs):
if not self.id:
self.slug = gen_slug(self.name)
super().save(*args, **kwargs)
def __str__(self):
return self.name
class ChecklistTaskCategory(models.Model):
task_category_name = models.CharField(max_length=250, unique=True, blank=True)
def __str__(self):
return self.task_category_name
class ChecklistTask(models.Model):
task = models.CharField(max_length=300, unique=True, blank=True)
description = models.TextField(max_length=500, blank=True, null=True)
checklist = models.ForeignKey(
Checklist,
on_delete=models.CASCADE,
blank=True,
null=True
)
task_category = models.ForeignKey(
ChecklistTaskCategory,
blank=True,
null=True,
on_delete=models.CASCADE)
done = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='task_done', blank=True)
slug = models.SlugField(max_length=250, unique=True, blank=True)
def get_done_url(self):
return reverse('task_done_toggle_url', kwargs={'slug': self.slug})
def get_absolute_url(self):
return reverse('checklist_url', kwargs={'slug': self.slug})
def save(self, *args, **kwargs):
if not self.id:
self.slug = gen_slug(self.task)
super().save(*args, **kwargs)
def __str__(self):
return self.task
{% for task in tasks %}
<div class="custom-control custom-switch pt-3" data-aos="fade-left">
<input type="checkbox" class="custom-control-input" id="customSwitch{{ task.id }}">
<label class="custom-control-label" for="customSwitch{{ task.id }}">{{ task.task }}</label>
</div>
{% endfor %}
答案 0 :(得分:0)
最好在 task_category 字段中添加 related_name 。
task_category = models.ForeignKey(
ChecklistTaskCategory,
blank=True,
null=True,
on_delete=models.CASCADE,
related_name='tasks')
并添加所有类别 ChecklistTaskCategory.objects.all()的列表以查看上下文。并获取模板中每个类别的任务列表。
{% for category in categories %}
<div>{{ category.task_category_name }} </div>
{% for task in category.tasks.all %}
<div class="custom-control custom-switch pt-3" data-aos="fade-left">
<input type="checkbox" class="custom-control-input" id="customSwitch{{ task.id }}">
<label class="custom-control-label" for="customSwitch{{ task.id }}">{{ task.task }}</label>
</div>
{% endfor %}
{% endfor %}