在Django中按降序排序类别

时间:2015-06-25 14:29:34

标签: python django

我正在尝试为诊所创建一个档案,我可以在每个专业领域展示医生。我想向大多数医生展示对大多数医生的专业化。

例如:

Dentists:
Names of all dentists (7)

Pediatricians
Names of pediatricians (4)

OB/GYN
Names of all obgyns (2)

models.py

class Doctor(models.Model):
    name = models.CharField(max_length=1300)
    specialization = models.ForeignKey(Specialization, related_name = "doctor_specialization")
    sub_specialization = models.ForeignKey(Specialization, related_name = "doctor_subSpecialization", null = True, blank = True)
    clinic = models.ForeignKey(Clinic)

class Clinic(models.Model):
        name = models.CharField(max_length=500)
        slug = models.CharField(max_length=200, blank = True, null = True, unique = True)
        contact_no = models.IntegerField() 

class Specialization(models.Model):
    name = models.CharField(max_length=30)

views.py

def clinicProfile(request, slug):
    clinic = get_object_or_404(Clinic, slug=slug)
    doctors = Doctor.objects.all().order_by('-netlikes')

    d.update({'clinic': clinic, 'doctors': doctors, })

    return render(request, 'm1/clinicprofile.html', d)

2 个答案:

答案 0 :(得分:1)

这很简单。只需使用('-netlikes')即可使用('netlikes'),它应该有效。你的功能应该是这样的。

 def clinicProfile(request, slug):
 clinic = get_object_or_404(Clinic, slug=slug)
 doctors = Doctor.objects.all().order_by('netlikes')

答案 1 :(得分:0)

您需要backward annotation,它看起来像是:

from django.db.models import Count, Min, Sum, Avg
specialization = Specialization.objects.annotate(Count('doctor'))