使用
order_by('?')将在生产的第二天杀死数据库服务器。更好的方法是类似于从关系数据库中获取随机行中所述的方法。from django.db.models.aggregates import Countfrom random import randintclass PaintingManager(models.Manager): def random(self): count = self.aggregate(count=Count('id'))['count'] random_index = randint(0, count - 1) return self.all()[random_index]


