Django显示平均温度

2024-04-24 21:40:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个博客,用户可以在我的帖子上留下评论/评价。我想显示该特定职位所有评分的平均值。我该怎么做?在

在模型.py在

class Post(models.Model):
    STATUS_CHOISES = (

        ('draft', 'Draft'),
        ('published', 'Published'),

        )
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique=True)
    content = models.TextField()
    seo_title = models.CharField(max_length=250)
    seo_description = models.CharField(max_length=160)
    author = models.ForeignKey(User, related_name='blog_posts', default=settings.AUTH_USER_MODEL)
    published = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=9, choices=STATUS_CHOISES, default='draft')

    def get_absolute_url(self):
        return reverse('blog:post_detail', args=[self.slug])


    def __str__(self):
        return self.title



class Comment(models.Model):
    difficulty_rating_choices = (

        (1, 'Very Easy'),
        (2, 'Easy'),
        (3, 'Moderate'),
        (4, 'Hard'),
        (5, 'Very Hard'),

    )

    workload_rating_choices = (

        (1, 'Very Light'),
        (2, 'Light'),
        (3, 'Moderate'),
        (4, 'Heavy'),
        (5, 'Very Heavy'),

    )

    book_rating_choices = (

        (1, '$'),
        (2, '$$'),
        (3, '$$$'),
        (4, '$$$$'),
        (5, '$$$$$'),

    )

    attendance_rating_choices = (

        (1, 'Not Required'),
        (2, 'Required'),


    )
    post = models.ForeignKey(Post, related_name="comments")
    user = models.CharField(max_length=250)
    email = models.EmailField()
    title = models.CharField(max_length=250)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    approved = models.BooleanField(default=False)
    difficulty_rating = models.IntegerField(choices=difficulty_rating_choices)
    workload_rating = models.IntegerField(choices=workload_rating_choices)
    book_rating = models.IntegerField(choices=book_rating_choices)
    attendance_rating = models.IntegerField(choices=attendance_rating_choices)


    def approved(self):
        self.approved = True
        self.save()

    def __str__(self):
        return self.title

在视图.py在

^{pr2}$

模板

<h1>Difficulty: <h1>
        <h1>Workload: <h1>
        <h1>Book Cost: <h1>
        <h1>Attendance: <h1>

        <hr>
        <h2>{{ post.title }}</h2>
        <p>Written by {{ post.author }} on {{ post.published }}</p>
        <hr>
        {{ post.content|linebreaks }}
        <hr>
        <h2>Comments</h2>
        <p>Total number of comments: {{ post.comments.count }}</p>
        <a href="{% url 'blog:add_comment' slug=post.slug %}">Leave a comment</a>
        {% for comment in post.comments.all %}


            <p>Title: {{ comment.title }}</p>
            <p>Review: {{ comment.body }}</p>
            <p>Date: {{ comment.created }}</p>
            <p>Posted by: {{ comment.user }}</p>
            <p>Difficulty: {{ comment.get_difficulty_rating_display }}</p>
            <p>Workload: {{ comment.get_workload_rating_display }}</p>
            <p>Book Cost: {{ comment.get_book_rating_display }}</p>
            <p>Attencance: {{ comment.get_attendance_rating_display }}</p>
            <hr>

        {% empty %}
            <p>There are no comments yet. Be the first one to comment!</p>

        {% endfor %}

我希望我的4个标准(难度评分,工作量评分,书本评分,出勤率评分)的平均值显示在模板顶部。我该怎么做呢?到目前为止我还没弄明白。。在

这是在模板级别完成的吗?我必须编辑我的views.py?或者是模型本身?在


Tags: selftruegettitlemodelscomment评分h1
1条回答
网友
1楼 · 发布于 2024-04-24 21:40:13

您可以在Post模型中编写helper方法:

from django.db.models import Avg

class Post(models.Model):
    # ...
    def avg_ratings(self):
        return self.comments.aggregate(
            Avg('difficulty_rating'),
            Avg('workload_rating'),
            Avg('book_rating'),
            Avg('attendance_rating'),
        )

然后在模板中可以执行以下操作:

^{pr2}$

相关问题 更多 >