1👍
✅
You can define a custom model manager for Entry
model.
class EntryManager(models.Manager):
#or any thing else you want
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]
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..
Source:stackexchange.com