Commits

Jacco Flenter committed 60e08b5

get_random method added to the manager...

Comments (0)

Files changed (1)

scm_core/models.py

     def exclude(self, *args, **kwargs):
         return self.filter().exclude(*args, **kwargs)
 
+    def get_random(self):
+
+        items = self.get_query_set().filter(category=category)
+        
+        count = items.count()
+
+	if count == 0:
+            raise self.model.DoesNotExist("%s matching query does not exist."
+                    % self.model._meta.object_name)
+
+        import random
+
+        return items[random.randint(0, count-1)]
+
 
 class PublishItem(models.Model):
     """Basic model for publish/unpublish and date related stuff