Get random object from django database model

Django -- Posted on March 11, 2019

Custom manager to get random object from django database model

              
                class RandomManager(models.Manager):

    def get_random(self, items=1):
        '''
        items is integer value
        '''
        if isinstance(items, int):
            return self.model.objects.order_by('?')[:items]
        return self.all()
                  
   
            

Related Posts