0

Here is the context: I have users, videos, topics, criterias and ratings

  • A video has a topic
  • A topic has criterias
  • A user can create a video for a given topic
  • A user can rate a video on the criterias given for the concerned topic.

Here is my model for that purpose:

RATE_CHOICES = zip( range(1,5), range(1,5) )

class VideoCrit(models.Model):
  """Criteria to rate videos on.
  Can be multiple for each Topic of Video"""
  name = models.CharField(max_length=50)
  def __unicode__(self):
    return self.name
  class Meta:
    verbose_name = 'Video Criteria'

class VideoTopic(models.Model):
  name = models.CharField(max_length=50)
  descr = models.TextField(blank=True, null=True)
  crits = models.ManyToManyField(VideoCrit,
    help_text='Criterias to rate the videos',
    blank=True, null=True,
  )
  def __unicode__(self):
    return self.name
  class Meta:
    verbose_name = 'Video Topic'

class VideoFile(models.Model):
  """Uploadable by users to be rated and commented"""
  name = models.CharField(max_length=50)
  descr = models.TextField(blank=True, null=True)
  file = models.FileField(upload_to='videos')
  topic = models.ForeignKey(VideoTopic)
  def __unicode__(self):
    return self.name
  class Meta:
    verbose_name = 'Chatter Tube'

class VideoRate(models.Model):
  """Users can Rate each Video on the criterias defined for the topic"""
  user = models.ForeignKey(User)
  video = models.ForeignKey(VideoFile)
  crit = models.ForeignKey(VideoCrit)
  rate = models.DecimalField(max_digits=2, decimal_places=1, choices=RATE_CHOICES)
  class Meta:
    unique_together = (('user', 'video', 'crit'),)
    verbose_name = 'Tube Rating'

Is it ok ?

If yes, from a template (DetailView based on the VideoFile class) for a given VideoFile, here is the interesting part of the template

  <div id="rating">
    <ul>
  {% for crit in videofile.topic.crits.all %}
      <li>
        <div class="rateit"
          crit-id="{{ crit.id }}"></div>
        {{ crit.name }}
      </li>
  {% endfor %}
    </ul>
  </div>

URLconf & View

#urlconf
#...
  (r'viewtube/(?P<pk>\d+)$', VideoFileDetailView.as_view()),
#...

#view
class VideoFileDetailView(DetailView):
  model = VideoFile
  def get_context_data(self, **kwargs):
    context = super(VideoFileDetailView, self).get_context_data(**kwargs)
#    context['rates'] = VideoRate.objects.filter(video=11)
    return context

How can I access to the ratings of the current logged user for the current video ?

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307

2 Answers2

1

UPDATE: To get all the ratings for the currently logged in user for the current video

# in Views.py 

video = VideoFile.objects.get(pk=video_id) #video_id is parameter sent from url
user_ratings = VideoRate.objects.filter(user=request.user).filter(video=video)

# in template
<ul>
{% for rating in user_ratings %}
    <li>{{ rating.crit.name }}: {{ rating.rate }}</li>
{% endfor %}
</ul>

PREVIOUSLY:

You should be able to access the ratings of the logged in user using something to this effect:

user.videorate_set.all

You can then display all of the ratings for a given user in your template as follows:

{% for rating in user.videorate_set.all %}
    {{ rating.video }} {{ ratings.rate }}
{% endfor %}
rolling stone
  • 12,668
  • 9
  • 45
  • 63
  • Sorry I updated my question. I'd like to see the ratings of the current user for the current video. – Pierre de LESPINAY Jun 20 '11 at 08:19
  • @Glide i see. if you want the ratings for just a single video for only the current logged in user its probably best to get those values in your view and then pass them as a variable to your template. can u post your other models, especially how you are connecting your User model with your other models, and also your view code? you could also write a custom template tag but that seems like it would be overkill. – rolling stone Jun 20 '11 at 15:58
  • This is giving me the rates of all the videos for this user, not only the current one. – Pierre de LESPINAY Jun 22 '11 at 06:23
  • @Glide are you including the `.filter(video=video)` part of the query? This should limit results to the video instance you specify. – rolling stone Jun 22 '11 at 17:00
  • Yes, in my extended DetailView I finally used `VideoRate.objects.filter(video=self.object, user=self.request.user)` I thought it were possible to access the rates within the template, without having to extend the view. – Pierre de LESPINAY Jun 23 '11 at 06:28
0

Django - Generic View Subclassed - url Parameters gave me the answer. I have to add the rates pre filtered to the context for the template.

class VideoFileDetailView(DetailView):
  model = VideoFile

  def get_context_data(self, **kwargs):
    context = super(VideoFileDetailView, self).get_context_data(**kwargs)
    context['rates'] = VideoRate.objects.filter(video=self.object, user=self.request.user)
    return context
Community
  • 1
  • 1
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307