gpt4 book ai didi

python - Django 中按降序排列类别

转载 作者:太空宇宙 更新时间:2023-11-03 17:37:13 24 4
gpt4 key购买 nike

我正在尝试为诊所创建一个个人资料,以便我可以向每个专业的医生展示。我想向最少医生展示最多医生的专业知识。

例如:

Dentists:
Names of all dentists (7)

Pediatricians
Names of pediatricians (4)

OB/GYN
Names of all obgyns (2)

模型.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)

View .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)

最佳答案

很简单。不用 ('-netlikes'),只需使用 ('netlikes') 就可以了。您的函数应该如下所示。

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

关于python - Django 中按降序排列类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31053277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com