I want to use the Django ORM to return 30 items even when less than 30 are in the database for that model. Is there a way to force it to return this many items even if it means returning duplicates?
projects = Project.objects.filter(approved=True).order_by('?')[0:30]
Is this possible? The above code returns 4 items as that’s how many I have in my database.
Would a better approach be to manipulate the projects variable to make it contain 30 items? I would like the projects randomised.
You’re fine if the result set is 30 or more objects, one way around it is to use
itertools.cyclefor where you don’t have enough… (which will start repeating data from the start again)… and limit it usingitertools.islice.