打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python3 manage.py迁移异常

我是django 1.7和python3的新手.我正在使用OSX.正如我在线关注django 1.7文档,

我试过了

python3 manage.py migrate

结果

Operations to perform:Apply all migrations: auth, contenttypes, sessions, adminRunning migrations:No migrations to apply.Traceback (most recent call last):  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/contrib/contenttypes/models.py", line 44, in get_for_modelct = self._get_from_cache(opts)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/contrib/contenttypes/models.py", line 34, in _get_from_cachereturn self.__class__._cache[self.db][key]KeyError: 'default'During handling of the above exception, another exception occurred:Traceback (most recent call last):  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in executereturn self.cursor.execute(sql, params)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 128, in executereturn self.cursor.execute(query, args)  File "/Users/NAME/Library/Python/3.4/lib/python/site-packages/MySQLdb/cursors.py", line 184, in executeself.errorhandler(self, exc, value)  File "/Users/NAME/Library/Python/3.4/lib/python/site-packages/MySQLdb/connections.py", line 37, in defaulterrorhandlerraise errorvalue  File "/Users/NAME/Library/Python/3.4/lib/python/site-packages/MySQLdb/cursors.py", line 171, in executer = self._query(query)  File "/Users/NAME/Library/Python/3.4/lib/python/site-packages/MySQLdb/cursors.py", line 330, in _queryrowcount = self._do_query(q)  File "/Users/NAME/Library/Python/3.4/lib/python/site-packages/MySQLdb/cursors.py", line 294, in _do_querydb.query(q)_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s AND `django_content_type`.`model` = %s) LIMIT 21' at line 1")The above exception was the direct cause of the following exception:Traceback (most recent call last):  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/contrib/contenttypes/models.py", line 50, in get_for_modeldefaults={'name': smart_text(opts.verbose_name_raw)},  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/manager.py", line 92, in manager_method    return getattr(self.get_queryset(), name)(*args, **kwargs)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/query.py", line 422, in get_or_create    return self.get(**lookup), False  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/query.py", line 351, in getnum = len(clone)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/query.py", line 122, in __len__self._fetch_all()  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/query.py", line 966, in _fetch_allself._result_cache = list(self.iterator())  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/query.py", line 265, in iteratorfor row in compiler.results_iter():  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 700, in results_iterfor rows in self.execute_sql(MULTI):  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sqlcursor.execute(sql, params)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/utils.py", line 81, in executereturn super(CursorDebugWrapper, self).execute(sql, params)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in executereturn self.cursor.execute(sql, params)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__six.reraise(dj_exc_type, dj_exc_value, traceback)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/utils/six.py", line 549, in reraiseraise value.with_traceback(tb)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in executereturn self.cursor.execute(sql, params)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 128, in executereturn self.cursor.execute(query, args)  File "/Users/NAME/Library/Python/3.4/lib/python/site-packages/MySQLdb/cursors.py", line 184, in executeself.errorhandler(self, exc, value)  File "/Users/NAME/Library/Python/3.4/lib/python/site-packages/MySQLdb/connections.py", line 37, in defaulterrorhandlerraise errorvalue  File "/Users/NAME/Library/Python/3.4/lib/python/site-packages/MySQLdb/cursors.py", line 171, in executer = self._query(query)  File "/Users/NAME/Library/Python/3.4/lib/python/site-packages/MySQLdb/cursors.py", line 330, in _queryrowcount = self._do_query(q)  File "/Users/NAME/Library/Python/3.4/lib/python/site-packages/MySQLdb/cursors.py", line 294, in _do_query    db.query(q)django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s AND `django_content_type`.`model` = %s) LIMIT 21' at line 1")During handling of the above exception, another exception occurred:Traceback (most recent call last):  File "manage.py", line 10, in <module>    execute_from_command_line(sys.argv)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_lineutility.execute()  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in executeself.fetch_command(subcommand).run_from_argv(self.argv)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argvself.execute(*args, **options.__dict__)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 338, in executeoutput = self.handle(*args, **options)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 164, in handleemit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/sql.py", line 268, in emit_post_migrate_signalusing=db)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/dispatch/dispatcher.py", line 198, in sendresponse = receiver(signal=self, sender=sender, **named)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/contrib/auth/management/__init__.py", line 83, in create_permissionsctype = ContentType.objects.db_manager(using).get_for_model(klass)  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/contrib/contenttypes/models.py", line 58, in get_for_model" is migrated before trying to migrate apps individually."RuntimeError: Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.`

我不知道这意味着什么,我相信这是不对的.
迁移是否成功?
请帮忙.谢谢

这是我的/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/contrib/contenttypes/models.py

from __future__ import unicode_literalsfrom django.apps import appsfrom django.db import modelsfrom django.db.utils import OperationalError, ProgrammingErrorfrom django.utils.translation import ugettext_lazy as _from django.utils.encoding import smart_text, force_textfrom django.utils.encoding import python_2_unicode_compatibleclass ContentTypeManager(models.Manager):    # Cache to avoid re-looking up ContentType objects all over the place.    # This cache is shared by all the get_for_* methods.    _cache = {}    def get_by_natural_key(self, app_label, model):        try:            ct = self.__class__._cache[self.db][(app_label, model)]        except KeyError:            ct = self.get(app_label=app_label, model=model)            self._add_to_cache(self.db, ct)        return ct    def _get_opts(self, model, for_concrete_model):        if for_concrete_model:            model = model._meta.concrete_model        elif model._deferred:            model = model._meta.proxy_for_model        return model._meta    def _get_from_cache(self, opts):        key = (opts.app_label, opts.model_name)        return self.__class__._cache[self.db][key]    def get_for_model(self, model, for_concrete_model=True):        """        Returns the ContentType object for a given model, creating the        ContentType if necessary. Lookups are cached so that subsequent lookups        for the same model don't hit the database.        """        opts = self._get_opts(model, for_concrete_model)        try:            ct = self._get_from_cache(opts)        except KeyError:            try:                ct, created = self.get_or_create(                    app_label=opts.app_label,                    model=opts.model_name,                    defaults={'name': smart_text(opts.verbose_name_raw)},                )            except (OperationalError, ProgrammingError):                # It's possible to migrate a single app before contenttypes,                # as it's not a required initial dependency (it's contrib!)                # Have a nice error for this.                raise RuntimeError(                    "Error creating new content types. Please make sure contenttypes"                      " is migrated before trying to migrate apps individually."                )            self._add_to_cache(self.db, ct)        return ct    def get_for_models(self, *models, **kwargs):        """        Given *models, returns a dictionary mapping {model: content_type}.        """        for_concrete_models = kwargs.pop('for_concrete_models', True)        # Final results        results = {}        # models that aren't already in the cache        needed_app_labels = set()        needed_models = set()        needed_opts = set()        for model in models:            opts = self._get_opts(model, for_concrete_models)            try:                ct = self._get_from_cache(opts)            except KeyError:                needed_app_labels.add(opts.app_label)                needed_models.add(opts.model_name)                needed_opts.add(opts)            else:                results[model] = ct        if needed_opts:            cts = self.filter(                app_label__in=needed_app_labels,                model__in=needed_models            )            for ct in cts:                model = ct.model_class()                if model._meta in needed_opts:                    results[model] = ct                    needed_opts.remove(model._meta)                self._add_to_cache(self.db, ct)        for opts in needed_opts:            # These weren't in the cache, or the DB, create them.            ct = self.create(                app_label=opts.app_label,                model=opts.model_name,                name=smart_text(opts.verbose_name_raw),            )            self._add_to_cache(self.db, ct)            results[ct.model_class()] = ct        return results    def get_for_id(self, id):        """        Lookup a ContentType by ID. Uses the same shared cache as get_for_model        (though ContentTypes are obviously not created on-the-fly by get_by_id).        """        try:            ct = self.__class__._cache[self.db][id]        except KeyError:            # This could raise a DoesNotExist; that's correct behavior and will            # make sure that only correct ctypes get stored in the cache dict.            ct = self.get(pk=id)            self._add_to_cache(self.db, ct)        return ct    def clear_cache(self):        """        Clear out the content-type cache. This needs to happen during database        flushes to prevent caching of "stale" content type IDs (see        django.contrib.contenttypes.management.update_contenttypes for where        this gets called).        """        self.__class__._cache.clear()    def _add_to_cache(self, using, ct):        """Insert a ContentType into the cache."""        # Note it's possible for ContentType objects to be stale; model_class() will return None.        # Hence, there is no reliance on model._meta.app_label here, just using the model fields instead.        key = (ct.app_label, ct.model)        self.__class__._cache.setdefault(using, {})[key] = ct        self.__class__._cache.setdefault(using, {})[ct.id] = ct@python_2_unicode_compatibleclass ContentType(models.Model):    name = models.CharField(max_length=100)    app_label = models.CharField(max_length=100)    model = models.CharField(_('python model class name'), max_length=100)    objects = ContentTypeManager()    class Meta:        verbose_name = _('content type')        verbose_name_plural = _('content types')        db_table = 'django_content_type'        ordering = ('name',)        unique_together = (('app_label', 'model'),)    def __str__(self):        # self.name is deprecated in favor of using model's verbose_name, which        # can be translated. Formal deprecation is delayed until we have DB        # migration to be able to remove the field from the database along with        # the attribute.        #        # We return self.name only when users have changed its value from the        # initial verbose_name_raw and might rely on it.        model = self.model_class()        if not model or self.name != model._meta.verbose_name_raw:            return self.name        else:            return force_text(model._meta.verbose_name)    def model_class(self):        "Returns the Python model class for this type of content."        try:            return apps.get_model(self.app_label, self.model)        except LookupError:            return None    def get_object_for_this_type(self, **kwargs):        """        Returns an object of this type for the keyword arguments given.        Basically, this is a proxy around this object_type's get_object() model        method. The ObjectNotExist exception, if thrown, will not be caught,        so code that calls this method should catch it.        """        return self.model_class()._base_manager.using(self._state.db).get(**kwargs)    def get_all_objects_for_this_type(self, **kwargs):        """        Returns all objects of this type for the keyword arguments given.        """        return self.model_class()._base_manager.using(self._state.db).filter(**kwargs)    def natural_key(self):        return (self.app_label, self.model)

解决方法:

我相信它是与mysql的依赖.切换到postgresql后,一切都解决了.我发现python与mysql的连接器只有python 3.3,我使用的是python3.4.这可能是原因,我找不到mysql和python 3.4的连接器.

来源:https://www.icode9.com/content-1-263301.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
如何在Django中创建自己的自定义用户模型?
django命令
python+django搭建web项目
Django实战: channels+celery+websocket打造聊天机器人(附源码)
Python django学习之:model (与数据库操作)
python测试开发django-157.celery异步与redis环境搭建
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服