任务是这样的:有医生,有专业。医生可以有几个专业。有必要通过专业化来得出医生名单的结论。
class Doctor(models.Model):
title = models.CharField(max_length=200, verbose_name='Имя')
slug = models.SlugField(unique=True)
image = models.ImageField(null=True, blank=True, verbose_name='Фото')
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name='Создано')
special = models.ManyToManyField('Specialization')
experience= models.CharField(max_length=200, verbose_name='Стаж', blank=True)
category = models.CharField(max_length=200, verbose_name='Категория', blank=True)
content = RedactorField(verbose_name='Контент')
keywords = models.CharField(max_length=1024, blank=True, null=True)
description = models.CharField(max_length=1024, blank=True, null=True)
class Meta:
verbose_name = 'Врача'
verbose_name_plural = 'Врачи'
def __str__(self): # For Python 2, use __unicode__ too
return self.slug
def get_absolute_url(self):
return reverse('show_doctor', kwargs={'slug': self.slug})
class Specialization(models.Model):
name = models.CharField(max_length=200, verbose_name='Название')
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name='Создано')
class Meta:
verbose_name = 'Специализацию'
verbose_name_plural = 'Специализации'
def __str__(self): # For Python 2, use __unicode__ too
return self.name
from django import template
from doctor.models import Doctor, Specialization
register = template.Library()
@register.inclusion_tag('doctors_list.html')
def get_doctors_list(request):
special_list = Specialization.objects.all()
return {
'doctors_list': Doctor.objects.filter(special=special_list),
'spec_list': special_list
}
{% for s in spec_list %}
<h3>{{ s.name }}</h3>
<ul>
{% for d in doctors_list %}
<li><a href="{% url 'doctor_detail' d.slug %}">{{ d.title }}</a></li>
{% endfor %}
</ul>
{% endfor %}
应该是:PrtSc。我无法理解,帮助我)
答案 0 :(得分:0)
当你为此目的使用模板标签时,你会以完全错误的方式使用Django。数据通过上下文变量传递给模板。在您的情况下,您在Specialization
个查询集上使用了ListView。您应该通过Specialization
模型(doctor_set
)中的反向关系访问医生。请为该查询集添加prefetch_related
该值。