python - Django 教程第 1 部分 'Poll' 对象没有属性 'was_published_today'

标签 python django django-models

我正在做这个 Django 教程 https://docs.djangoproject.com/en/dev/intro/tutorial01/并_发布_今天不起作用。感谢您抽出时间。

这是命令行:

    Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on
    win32
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> from polls.models import Poll, Choice
    >>> Poll.objects.all()
    [<Poll: What's up?>]
    >>> Poll.objects.get(pk=1)
    <Poll: What's up?>
    >>> p = Poll.objects.get(pk=1)
    >>> p.was_published_today()
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
    AttributeError: 'Poll' object has no attribute 'was_published_today'

这里是 models.py

    # Create your models here.
    from django.db import models

    import datetime

    class Poll(models.Model):
        question = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
        def __unicode__(self):
            return self.question
        def was_published_today(self):
            return self.pub_date.date() == datetime.date.today()


    class Choice(models.Model):
        poll = models.ForeignKey(Poll)
        choice = models.CharField(max_length=200)
        votes = models.IntegerField()
        # ...
        def __unicode__(self):
            return self.question

    >>> dir (p)
    ['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict_
    _', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__
    ', '__metaclass__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_e
    x__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_
    _unicode__', '__weakref__', '_base_manager', '_collect_sub_objects', '_default_m
    anager', '_deferred', '_get_FIELD_display', '_get_next_or_previous_by_FIELD', '_
    get_next_or_previous_in_order', '_get_pk_val', '_get_unique_checks', '_meta', '_
    perform_date_checks', '_perform_unique_checks', '_set_pk_val', '_state', 'choice
    _set', 'clean', 'clean_fields', 'date_error_message', 'delete', 'full_clean', 'g
    et_next_by_pub_date', 'get_previous_by_pub_date', 'id', 'objects', 'pk', 'prepar
    e_database_save', 'pub_date', 'question', 'save', 'save_base', 'serializable_val
    ue', 'unique_error_message', 'validate_unique']

注意:我也在使用 instantdjango.com 的 Instant Django。

最佳答案

我在学习 Django 教程时遇到了类似的问题。我发现,为了选择自定义方法,我必须退出 Python shell 并重新启动它(并重新导入 Poll 类)。

关于python - Django 教程第 1 部分 'Poll' 对象没有属性 'was_published_today',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6641212/

相关文章:

python - 基础 Python - 查找列表中元素之间的差异 - IndexError

python - 迁移中的 Django 设置

django - 即使部署后,在 Azure 上部署 Django 应用程序 : only displays default app,

python - 使用外部 POST 请求绕过 Django CSRF 保护

python - 获取跨越关系的对象的计数

python - python中将迭代器转换为List

python - 是否可以监视 forked linux 进程的写时复制? (特别是 python )

python - 如何以编程方式创建 django ViewFlow 进程

django - 如何通过Django中的表在manytomany中添加自定义字段

django - 管理应用程序中 Django 模型多表继承中的左连接