说我的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
。
那么我该如何巧妙地实现这一点呢?
答案 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)
只能进行有效的民意调查。