title: Django generic listview filter utilizing current user id
body: <p>I'm creating a rather simple web-app that'll allow me to rate various items (hopefully for use in generating some machine learning recommendations).</p>
  
  <p>I've gotten the 5-star rating working by using <a href="https://github.com/dcramer/django-ratings" rel="nofollow noreferrer">Django-ratings</a>, I'm having difficulties creating new views for my 'rated items', however. More specifically: I'm getting stuck on attempting to create an 'unrated item'-view and 'previously rated items' view.</p>
  
  <p>The djangoratings works by creating the using the following model to administer votes:</p>
  
  <pre><code>class Vote(models.Model):
      content_type    = models.ForeignKey(ContentType, related_name="votes")
      object_id       = models.PositiveIntegerField()
      key             = models.CharField(max_length=32)
      score           = models.IntegerField()
      user            = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name="votes")
      ip_address      = models.IPAddressField()
      cookie          = models.CharField(max_length=32, blank=True, null=True)
      date_added      = models.DateTimeField(default=now, editable=False)
      date_changed    = models.DateTimeField(default=now, editable=False)
  
  class ContentType(models.Model):
      name = models.CharField(max_length=100)
      app_label = models.CharField(max_length=100)
      model = models.CharField(_('python model class name'), max_length=100)
      objects = ContentTypeManager()
  </code></pre>
  
  <p>The items I'm rating (models.py):</p>
  
  <pre><code>class Media(models.Model):
      id = models.AutoField(primary_key=True)
      url = models.CharField(unique=True, max_length=100)
      last_updated = models.DateTimeField(blank=True, null=True)
      ...
      rating = RatingField(range=5, can_change_vote=True, allow_delete=True)  # 5 possible rating values, 1-5
      class Meta:
          verbose_name_plural = "Media"
  </code></pre>
  
  <p>The following view works lovely by paginating and allowing me to rate each item:</p>
  
  <pre><code>class MediaListView(LoginRequiredMixin, ListView):
      queryset = Media.objects.all()
      template_name = 'myMedia/media_list.html'
      paginate_by = 10
  </code></pre>
  
  <p>However, I'm looking for a way to filter these items in two ways:</p>
  
  <ul>
  <li>Retrieve only rated views, ordered by rating date (DESC)</li>
  <li>Retrieve only unrated views, not (yet, maybe later!) ordered.</li>
  </ul>
  
  <p>I figured I need to query the Vote table (filtering on the current user) to do so. As I don't have anything other than 'media' rated I can ignore the 'content_type' aspect of the generic-ratings table (which takes care of different types of models being rated). </p>
  
  <p>I see <a href="https://stackoverflow.com/questions/24423626/django-form-queryset-filter-by-current-user">request.user</a> mentioned quite often in questions related to identifying the current user. I am, however, getting a 'NameError: name 'request' is not defined' error when trying to do so:</p>
  
  <pre><code>#views.py
  class MediaListView(LoginRequiredMixin, ListView):
      queryset = Media.objects.all()
      template_name = 'myMedia/media_list.html'
      paginate_by = 10
  
  class MediaListView_VotesByUser(LoginRequiredMixin, ListView):
      User = request.user
      voted_ids = Vote.objects.filter(user=User).values('object_id', flat=True) # Obtain the IDs for voted items.
      queryset = Media.objects.filter(id__in=voted_ids])
      paginate_by = 10
  
  class MediaListView_Unrated(LoginRequiredMixin, ListView):
      User = request.user
      voted_ids = Vote.objects.filter(user=User).values('object_id', flat=True) # Obtain the IDs for voted items.
      queryset = Media.objects.exclude(id__in=voted_ids])
      paginate_by = 10
  
  
  # urls.py
  urlpatterns = patterns('',
      url(
          regex=r'^$',
          view=views.MediaListView.as_view(),
          name='media'),
      url(r'^rate/(?P&lt;object_id&gt;\d+)/(?P&lt;score&gt;\d+)/', AddRatingFromModel(), {
          'app_label': 'myMedia',
          'model': 'media',
          'field_name': 'rating',
      }),
      url(
          regex=r'^myVotes',
          view=views.MediaListView_VotesByUser.as_view(),
          name='UserVotedOn'),
      url(
          regex=r'^unrated',
          view=views.MediaListView_Unrated.as_view(),
          name='UnratedMedia')
  
  )
  </code></pre>
  
  <p>Furthermore, I'm not quite sure I'm heading into the right direction. Do I need separate views for this or extend the existing view? Any help or tips on how I'd create these views would be greatly appreciated. I'm working with Django 1.7 (with MySQL), if that matters.</p>