[Answer]-Checking for boolean/null to sort in Django sqlite3

1👍

You can define a custom model manager for Entry model.

class EntryManager(models.Manager):
def unvoted_or_random(self):
unvoted_entries = self.filter(voted = False).order_by('-pub_date')
if unvoted_entries:
return unvoted_entries[:1]
else:
return self.all()[:1]
#or any thing else you want

and then in your Entry model you can do something like.

class Entry(models.Model):
...
objects = EntryManager()

and then you can get your required entries in the views by

Entry.objects.unvoted_or_random()

and pass it into the context..

I hope it would help..

Leave a comment