I have the following model —
class VideoCredit(models.Model):
video = models.ForeignKey(VideoInfo)
profile = models.ForeignKey('UserProfile', blank=True, null=True)
normalized_name = models.CharField(max_length=100)
I want to order credits by whether they have a profile, and then by noramlized_name, for example —
# 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]
I tried doing ordering=['-profile_full_name', 'normalized_name'], but it would order profile first, but in reverse alphabetical order. If I did profile_full_name instead, it would return those without a profile (a NULL field), at the beginning. How would I do this? Thank you.
Update: I got it down to this, which is working nicely:
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, ''))
1 Answer