基于另一个类python / django的启动来增加一个类的变量

时间:2018-08-29 17:55:03

标签: python django

我的目标是显示特定比赛的参赛总数。最初,我尝试在模板部分中通过在每次进入条目时增加每个比赛的计数器来做到这一点。我的想法是,当创建参赛作品提交时,对应比赛的total_submissions应该增加,但是我不确定该怎么做。

型号:

class Contest(models.Model):
    contest_name = models.CharField(max_length=200)
    contest_description = models.CharField(max_length=5000)
    contest_start_date = models.DateTimeField('start date')
    contest_end_date = models.DateTimeField('end date')
    submission_count = models.IntegerField(default=0)

    def __str__(self):
        return self.contest_name


class Submission(models.Model):
    contest = models.ForeignKey(Contest, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
    date_uploaded = models.DateTimeField(auto_now_add=True)
    votes = models.IntegerField(default=0)

    def __init__(self):
        #increment the submission_count variable for the corresponding contest

使用模板的其他尝试:

<h1>Current Contests</h1>
<div class="row">
    {% if current_contest_list %}
        {% for contest in current_contest_list %}
            <div class="col-sm-4"><h1><a href="{% url 'contests:detail' contest.id%}">{{ contest.contest_name }}</a></h1>
                <img src="media/{{contest.display_image}}" class="img-responsive" alt="image">
                {{ contest.contest_name }}

                {% for submission in submission_list %}

                    {% if submission.contest == contest.contest_name %}
                        <P>Hi!</P>
                    {% endif %}
                    <p>{{ submission.contest }} {{ contest.contest_name }}</p>
                    {% if submission.contest == contest.contest_name %}
                        <P>Hi!</P>
                        {{ forloop.counter }}
                        {% set total_submissions = forloop.counter %}
                    {% endif %}


                {% endfor %}

    <p>Total entries: {{total_submissions}} </p>

当我这样做时,total_submissions变量没有增加。我想念什么?

2 个答案:

答案 0 :(得分:1)

据我了解,您想在添加新提交时增加比赛的提交数量。为此,您需要

  1. 在数据库中插入新的submission_count时增加Submission
  2. 删除submission_count后减少Submission

要执行(1),您可以覆盖save()的{​​{1}}方法:

Submission

要执行(2),您可以覆盖 def save(self, *args, **kwargs): if self.id is None: # To make sure this is an INSERT, not an UPDATE contest = Contest.objects.select_for_update().get(id=self.contest.id) contest.submission_count += 1 contest.save() super().save(*args, **kwargs) # Call the "real" save() method. 方法:

delete()

请注意,如果您批量创建或删除提交,此解决方案将无法使用。在https://docs.djangoproject.com/en/2.1/topics/db/models/#overriding-predefined-model-methods

中查看更多信息

个人意见:我不会将提交计数存储在db中,除非查询计数提交数量的查询对性能有明显影响。

答案 1 :(得分:0)

Django确实不鼓励在模板内更改数据,并且这样做非常困难。您尝试执行的操作无法在模板中使用。

在您的模板代码中,submission_list来自哪里?如果您要遍历contest_list中每个竞赛中的所有提交内容,则可以执行以下操作:

 {% for submission in contest.submission_set.all %}

要获取计数,如果您的列表实际上是一个查询集(通常是Queryset),那么在模板中获取计数的最简单方法是在查询集上使用count,例如:

{{ contest.submission_set.all.count }}

这将导致另一个数据库查询,但是如果您不担心访问者数量过多,应该没问题。

相关问题