python_2_unicode_compatible 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20741754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:08:03  来源:igfitidea点击:

python_2_unicode_compatible error

pythondjangopython-2.7ubuntudjango-models

提问by kmario23

I've models.pyas follows,

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

When I ran python manage.py syncdbthis is the error I got:

当我运行时,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

I don't know why the module is not imported. I'm using Python 2.7 and Django 1.4.

我不知道为什么没有导入模块。我使用的是 Python 2.7 和 Django 1.4。

采纳答案by yetty

python_2_unicode_compatiblefeature has only been added in Django 1.5 version.

python_2_unicode_compatible此功能仅在 Django 1.5 版本中添加。

https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.encoding.python_2_unicode_compatible

https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.encoding.python_2_unicode_compatible

回答by Tim Graham

It's actually also present in the 1.4 series since 1.4.2. You should really be using the latest 1.4.X release (1.4.10 as of the time of this writing) as earlier versions have known security vulnerabilities.

从 1.4.2 开始,它实际上也出现在 1.4 系列中。您真的应该使用最新的 1.4.X 版本(在撰写本文时为 1.4.10),因为早期版本存在已知的安全漏洞。

回答by mre

I ran into this issue when I wanted to use Django for Graphite. Turns out I had Django 1.3 installed and my Graphite version was breaking with Django > 1.5, so installing the latest version of the 1.4 branch fixed the problem:

当我想将 Django 用于 Graphite 时,我遇到了这个问题。结果我安装了 Django 1.3 并且我的 Graphite 版本在 Django > 1.5 时崩溃了,所以安装最新版本的 1.4 分支解决了这个问题:

sudo pip install --upgrade 'Django<1.5'

回答by Desperad0

try

尝试

from django.utils.six import python_2_unicode_compatible

instead of

代替

from django.utils.encoding import python_2_unicode_compatible

this works well for me in Django 1.10.6

这在 Django 1.10.6 中对我很有效

回答by goose

For the latest Django 3.0.4 , and auditlog try

对于最新的 Django 3.0.4 和 auditlog 试试

from six import python_2_unicode_compatible

instead of

代替

from django.utils.six import python_2_unicode_compatible

if it is not install run the below code

如果没有安装运行下面的代码

pip install six