0

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. 但它保持设置为False仅用于这一个 shell 会话。

那么我怎样才能巧妙地实现呢?

4

1 回答 1

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)

这只会获得您的活跃民意调查。

于 2013-09-12T16:08:19.293 回答