Django 1.11.4中的错误('datetime.datetime'对象没有属性'split') [英] error ('datetime.datetime' object has no attribute 'split') in django 1.11.4

查看:401
本文介绍了Django 1.11.4中的错误('datetime.datetime'对象没有属性'split')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过官方文档上的教程学习django版本1.11.4.我正在使用python 3.6.5和mysql8作为数据库.我也使用mysql.connector.django连接到mysql数据库.我尝试制作第一个Django应用程序,第2部分.

I am learning django version 1.11.4 through tutorial on the official documentation. I am using python 3.6.5 and mysql8 for database. I also use mysql.connector.django to connect to mysql database. I tried to do the first Django app, part 2.

这是我使用的示例的链接

一切正常,除非我运行以下命令:

everything works fine except when I run the this command:

Question.objects.all()

Question.objects.all()

我遇到以下错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 226, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __iter__
    self._fetch_all()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py", line 62, in __iter__
    for row in compiler.results_iter(results):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 839, in results_iter
    for rows in results:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1284, in cursor_iter
    sentinel):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1283, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)),
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/utils.py", line 101, in inner
    return func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/cursor_cext.py", line 510, in fetchmany
    rows.extend(self._cnx.get_rows(size))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 275, in get_rows
    row[i])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/conversion.py", line 205, in to_python
    return self._cache_field_types[vtype[1]](value, vtype)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/django/base.py", line 119, in _DATETIME_to_python
    dt = MySQLConverter._DATETIME_to_python(self, value)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mysql/connector/conversion.py", line 506, in _DATETIME_to_python
    (date_, time_) = value.split(b' ')
AttributeError: 'datetime.datetime' object has no attribute 'split'

模型文件中使用的代码:

the code used in the models file :

import datetime

from django.db import models

from django.utils import timezone

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - 
        datetime.timedelta(days=1)

    def __str__(self):
        return self.question_text



class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
    return self.choice_text

数据库设置:

DATABASES = {
    'default': {
        'NAME': 'mysite',
        'ENGINE': 'mysql.connector.django',
        'USER': 'root',
        'PASSWORD': '********',
        'OPTIONS': {
          'autocommit': True,
        },
    }
}

任何解决此错误的线索或帮助将不胜感激.

any clue or help to fix this error will be appreciated.

推荐答案

@ Alasdair提供的解决方案是:

DATABASES['default']['OPTIONS']中设置use_pure=True.

这篇关于Django 1.11.4中的错误('datetime.datetime'对象没有属性'split')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆