daily pastebin goal
85%
SHARE
TWEET

Untitled

a guest Jan 16th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class UserRating(models.Model):
  2.  
  3.     RATING_CATEGORIES = (
  4.         1: 'Bad',
  5.         2: 'Boring',
  6.         3: 'Average',
  7.         4: 'Good',
  8.         5: 'Great'
  9.     )
  10.  
  11.     for_user = models.ForeignKey(User, related_name='for_user')
  12.     by_user = models.ForeignKey(User, related_name='by_user')
  13.     rating_category = models.IntegerField(choices=RATING_CATEGORIES)
  14.     points = models.IntegerField(_('Points'), default=0)
  15.     created = models.DateTimeField(_('Created'))
  16.    
  17. entries = UserRating.objects.values('rating_category').filter(
  18.         for_user=for_user,
  19.         by_user=by_user).order_by('-created')[:5]
  20.    
  21. id for_user by_user rating_category points         created
  22.  1    1        1           1          100     2012-09-28 00:19:00
  23.  2    1        1           2          100     2012-09-28 00:18:00
  24.  3    1        1           4          100     2012-09-28 00:17:00
  25.  4    1        1           4           0      2012-09-28 00:16:00
  26.  5    1        1           3          100     2012-09-28 00:15:00
  27.  6    1        1           5           0      2012-09-27 00:19:00
  28.  7    1        1           2           0      2012-09-26 00:18:00
  29.    
  30. from django.db.models import Max
  31. entries = (UserRating.objects.filter(for_user = for_user, by_user = by_user)
  32.            .values('rating_category')
  33.            .annotate(last_created = Max('created'))
  34.            .order_by('-last_created'))[:5]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top