我有models.py
如下,
from django.contrib.auth.models import User from django.db import models from django.utils.encoding import python_2_unicode_compatible from django.utils.timezone import now @python_2_unicode_compatible class Tag(models.Model): name = models.CharField(max_length=50, unique=True) class Meta: verbose_name = 'tag' verbose_name_plural = 'tags' ordering = ['name'] def __str__(self): return self.name ............. and so on
当我跑了python manage.py syncdb
这是我得到的错误:
itman@itman:~/djangoApp/mysite$ python manage.py syncdb Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line utility.execute() File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 196, in run_from_argv self.execute(*args, **options.__dict__) File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 231, in execute self.validate() File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 266, in validate num_errors = get_validation_errors(s, app) File "/usr/lib/python2.7/dist-packages/django/core/management/validation.py", line 30, in get_validation_errors for (app_name, error) in get_app_errors().items(): File "/usr/lib/python2.7/dist-packages/django/db/models/loading.py", line 158, in get_app_errors self._populate() File "/usr/lib/python2.7/dist-packages/django/db/models/loading.py", line 67, in _populate self.load_app(app_name) File "/usr/lib/python2.7/dist-packages/django/db/models/loading.py", line 88, in load_app models = import_module('.models', app_name) File "/usr/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module __import__(name) File "/home/itman/djangoApp/mysite/bmark/models.py", line 4, in <module> from django.utils.encoding import python_2_unicode_compatible ImportError: cannot import name python_2_unicode_compatible
我不知道为什么模块没有导入。 我正在使用Python 2.7和Django 1.4。
python_2_unicode_compatible
功能只在Django 1.5版本中添加。
https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.encoding.python_2_unicode_compatible
当我想为Django使用Graphite时遇到了这个问题。 事实证明,我安装了Django 1.3,并且我的Graphite版本与Django> 1.5分开,所以安装1.4分支的最新版本修复了这个问题:
sudo pip install --upgrade 'Django<1.5'
它实际上也出现在1.4.2以后的1.4系列中。 您应该真正使用最新的1.4.X版本(本文撰写时为1.4.10),因为早期版本已知安全漏洞。