python - Django 要求我安装 pytz,现在我的模型无法验证 Django

标签 python django model pytz

我在测试刚设置的项目时遇到此错误

Exception Type: ImproperlyConfigured
Exception Value:    
This query requires pytz, but it isn't installed.

经过一些谷歌搜索后,我发现我得到了这个,因为我在我的设置中指定了

USE_TZ = True

我认为这应该让我的项目时区感知。

所以我运行了 sudo pip install pytz 来安装 pytz,所以我不会得到这个错误。问题是现在,安装了 pytz 后,当我尝试执行 ./manage.py runserver 时,我的模型将无法验证,我收到此错误

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
    from django.utils.log import configure_logging
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/utils/log.py", line 10, in <module>
    from django.views.debug import ExceptionReporter, get_exception_reporter_filter
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/views/debug.py", line 10, in <module>
    from django.http import (HttpResponse, HttpResponseServerError,
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/http/__init__.py", line 4, in <module>
    from django.http.response import (HttpResponse, StreamingHttpResponse,
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/http/response.py", line 13, in <module>
    from django.core.serializers.json import DjangoJSONEncoder
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/core/serializers/__init__.py", line 23, in <module>
    from django.core.serializers.base import SerializerDoesNotExist
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/core/serializers/base.py", line 6, in <module>
    from django.db import models
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/db/models/__init__.py", line 6, in <module>
    from django.db.models.query import Q, QuerySet, Prefetch  # NOQA
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/db/models/query.py", line 13, in <module>
    from django.db.models.fields import AutoField, Empty
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 15, in <module>
    from django.db.models.lookups import default_lookups, RegisterLookupMixin
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/db/models/lookups.py", line 6, in <module>
    from django.utils import timezone
  File "/Users/user/.virtualenvs/app/lib/python3.4/site-packages/django/utils/timezone.py", line 149, in <module>
    utc = pytz.utc if pytz else UTC()
AttributeError: 'module' object has no attribute 'utc'

这里发生了什么?为什么会出现此错误?在我看来,问题出在 pytz 上,但我不确定。任何帮助将不胜感激。

这是我的模型,在安装 pytz 之前运行良好

from django.db import models
from django.conf import settings

# Create your models here.
class Item(models.Model):
    title = models.CharField(max_length=100, )
    description = models.TextField()
    seller = models.ForeignKey(settings.AUTH_USER_MODEL)
    price = models.DecimalField(max_digits=11, decimal_places=2)
    timestamp = models.DateTimeField(auto_now_add=True)
    last_updated = models.DateTimeField(auto_now=True)


    def __str__(self):
        return self.title

编辑:我添加了我的 views.py 文件

from .models import Item

from django.views.generic import ListView, DetailView

class ItemListView(ListView):
    model = Item

class ItemDetailView(DetailView):
    model = Item

    def get_context_data(self, **kwargs):
       # Call the base implementation first to get a context
       #This is getting a dictionary of the objects being displayed to
       #the template
       context = super(ItemDetailView, self).get_context_data(**kwargs)
       # Add in a QuerySet of all items up for trade by the user
       context['other_things_for_trade_by_user'] = Item.objects.filter(seller=context['seller'])
       return context

最佳答案

正如所建议的那样,此问题是由 pytz 和 django 的版本不匹配引起的。这是通过卸载 pytz 和 django 并使用 pip 重新安装它们来解决的。

关于python - Django 要求我安装 pytz,现在我的模型无法验证 Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26710994/

相关文章:

ruby-on-rails - 保存 Rails 模型后重定向 View

python - 将数据从 excel 电子表格导入到 django 模型

Python 2.7 - 如何以编程方式从 stdin 读取二进制数据

python - Numpy 沿一个轴提取任意子数组

python - ImportError : No module named grappellidjango. 贡献

django - 如何集成 Django 和 react-router-dom?

python - 如何从电子邮件中获取 csv 附件并保存

python - 忽略传递的参数

python - 在 INSTALLED_APPS 中添加 "moderation"时出错

mysql - 更新 Eloquent 模型的 JSON 字段