Jam*_* L. 0 python sorting django
我有一个医生模型,我正在显示基于最喜欢的医生对象.如果医生是否获得赞助,我会为每位医生提供一个布尔字段.我想首先向赞助医生展示,然后根据最喜欢的人对其余的医生进行分类.
这是我要按netlikes排序的,它正在工作
doctors = Doctor.objects.all().order_by('-netlikes')
Run Code Online (Sandbox Code Playgroud)
我试过这个,但没有任何区别
doctors = Doctor.objects.all().order_by('sponsored').order_by('-netlikes')
Run Code Online (Sandbox Code Playgroud)
医生model.py
class Doctor(models.Model):
name = models.CharField(max_length=1300)
title = models.CharField(max_length=1300, null = True, blank = True)
specialization = models.ForeignKey(Specialization)
clinic = models.ForeignKey(Clinic)
education1 = models.CharField(max_length=1300)
gender_choices = ( ('Male', 'Male'), ('Female','Female'),)
gender = models.CharField(max_length=15, choices = gender_choices, null=True, blank = True)
image = models.ImageField(upload_to='uploads/', null=True, blank = True)
likes = models.IntegerField(default=0)
dislikes = models.IntegerField(default=0)
netlikes = models.IntegerField(default=0)
submitted_on = models.DateTimeField(auto_now_add=True, null = True, blank = True)
sponsored = models.BooleanField(default = False)
Run Code Online (Sandbox Code Playgroud)
关于我如何首先展示赞助医生然后最高的喜欢?
你不能order_by在django中链接,最后一个会覆盖以前的所有排序.为了避免它,只需使用
doctors = Doctor.objects.all().order_by('sponsored', '-netlikes')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4341 次 |
| 最近记录: |