问题
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 ?
回答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 %}
回答2:
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
来源:https://stackoverflow.com/questions/6388259/django-rating-model-example-detailview-template