如何在Django'关闭'民意调查

时间:2013-09-12 16:04:31

标签: python django

说我的Poll中的models.py个对象有这个课程:

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):
        return self.question

    # Opened by default.
    is_opened = True

    def close_poll(self):
        self.is_opened = False
         # Don't know how to implement this...

    def open_poll(self):
        self.is_opened = True
        # Don't know how to implement this...

    def pub_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date < now
    pub_recently.admin_order_field = 'pub_date'
    pub_recently.boolean = True
    pub_recently.short_description = 'Published recently?'

当我在close_poll中使用python manage.py shell函数时,它会将is_opened变量设置为False。但它仅为此一个shell会话设置为False

那么我该如何巧妙地实现这一点呢?

1 个答案:

答案 0 :(得分:2)

如果我理解这一点,你应该这样做:

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    is_active = models.BooleanField(default=True)

因此,在执行查询时,请执行以下操作:

Poll.objects.filter(is_active=True)

只能进行有效的民意调查。