我有以下型号 -
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, ''))
答案 0 :(得分:0)
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, ''))