django-admin2/example/polls/models.py

48 lines
1.4 KiB
Python
Raw Normal View History

2013-07-07 14:13:48 +00:00
# -*- coding: utf-8 -*-
from __future__ import division, absolute_import, unicode_literals
2013-06-05 02:06:33 +00:00
import datetime
2013-06-05 01:10:59 +00:00
from django.db import models
2013-06-05 02:06:33 +00:00
from django.utils import timezone
2016-05-07 23:31:16 +00:00
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
2013-06-05 01:10:59 +00:00
2014-11-26 15:42:17 +00:00
@python_2_unicode_compatible
class Poll(models.Model):
question = models.CharField(max_length=200, verbose_name=_('question'))
pub_date = models.DateTimeField(verbose_name=_('date published'))
2014-11-26 15:42:17 +00:00
def __str__(self):
return self.question
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
2013-06-05 01:59:15 +00:00
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = _('Published recently?')
class Meta:
verbose_name = _('poll')
verbose_name_plural = _('polls')
2014-11-26 15:42:17 +00:00
@python_2_unicode_compatible
class Choice(models.Model):
poll = models.ForeignKey(
Poll,
verbose_name=_('poll'),
on_delete=models.CASCADE
)
2014-11-26 15:42:17 +00:00
choice_text = models.CharField(
max_length=200, verbose_name=_('choice text'))
votes = models.IntegerField(default=0, verbose_name=_('votes'))
2014-11-26 15:42:17 +00:00
def __str__(self):
return self.choice_text
class Meta:
verbose_name = _('choice')
verbose_name_plural = _('choices')