Pull random records using Django ORM.
- Python 3.8+
- Django 3.2+
$ pip install django-random-queryset
Add RandomManager
to desired model:
from django.db import models
from django_random_queryset import RandomManager
class Model(models.Model):
objects = RandomManager()
# ...
No database migrations are needed.
How to use it:
queryset = Model.objects.filter(field=value)
queryset.random() # to get one random record
queryset.random(5) # to pass limited random records
queryset.random(len(queryset)) # to get all random records
queryset.random().values() # to have access to other queryset methods