0
我有以下的模型 -訂購模型
class VideoCredit(models.Model):
video = models.ForeignKey(VideoInfo)
profile = models.ForeignKey('UserProfile', blank=True, null=True)
normalized_name = models.CharField(max_length=100)
我要訂購通過他們是否有一個配置文件,然後通過noramlized_name,例如學分 -
# for the following entries
id video profile normalized_name
1 Terminator Terry Gilliam Terry Gilliam
2 Terminator James Cameron James Cameron
3 Dracula Null Bela Lugosi
# it would order as: [James Cameron, Terry Gilliam, Bela Lugosi]
我試着做ordering=['-profile_full_name', 'normalized_name']
,但它會先排序,但按字母順序排列。如果我改爲profile_full_name
,它會在開始時返回沒有配置文件(NULL字段)的那些。我將如何做到這一點?謝謝。
更新:我下來到這一點,這是很好的工作:
recent_activity=[]
for object in RecentActivity.objects.select_related.all()[:50]:
if object.event_type == 2:
credits_yes_profile = object.content_object.videocredit_set.exclude(profile=None).order_by('profile__full_name')
credits_no_profile = object.content_object.videocredit_set.filter(profile=None).order_by('normalized_name')
sorted_credits = list(credits_yes_profile) + list(credits_no_profile)
recent_activity.append((object, sorted_credits))
else:
recent_activity.append((object, ''))
看http://stackoverflow.com/questions/5235209/django-order-by-position -ignoring空 – shanyu