Django 1.6:按布尔和喜欢对对象进行排序

时间:2014-09-24 22:02:17

标签: python django sorting

我有一个医生模型,我根据最喜欢的方式显示医生对象。如果医生是否获得赞助,我会为每位医生提供一个布尔字段。我想首先向赞助医生展示,然后根据最喜欢的医生对其余医生进行分类。

这是我要按netlikes排序并且它正在工作

doctors = Doctor.objects.all().order_by('-netlikes')

我试过这个,但没有任何区别

doctors = Doctor.objects.all().order_by('sponsored').order_by('-netlikes')

医生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)

有关如何首先向受资助的医生展示最喜欢的医生的想法?

1 个答案:

答案 0 :(得分:6)

你不能在django中链接order_by,最后一个会覆盖所有先前的排序。要避免它,只需使用

doctors = Doctor.objects.all().order_by('sponsored', '-netlikes')