error ('datetime.datetime' object has no attribute 'split') in django 1.11.4 - mysql

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.
This is the link of the example I used
everything works fine except when I run the this command:
Question.objects.all()
I got the following error:
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
the database setting:
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.

I struggled for few hours to set up my Django project with python3 using MySQL DB on MacOS.
I was not able to install either mysqlclient and MySQL-Python by pip3 in a virtual environment created with virtualenv
error stacktrace was: something wrong due to configparser in python3
Jans-MacBook-Pro:~ jan$ /Library/Frameworks/Python.framework/Versions/3.3/bin/pip-3.3 install MySQL-python
Downloading/unpacking MySQL-python
Running setup.py egg_info for package MySQL-python
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python/setup.py", line 14, in <module>
from setup_posix import get_config
File "./setup_posix.py", line 2, in <module>
from ConfigParser import SafeConfigParser
ImportError: No module named 'ConfigParser'
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python/setup.py", line 14, in <module>
from setup_posix import get_config
File "./setup_posix.py", line 2, in <module>
from ConfigParser import SafeConfigParser
ImportError: No module named 'ConfigParser'
----------------------------------------
Command python setup.py egg_info failed with error code 1 in /var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python
Storing complete log in /Users/jan/.pip/pip.log
Jans-MacBook-Pro:~ jan$
Now the SOLUTION which worked for me was
1) installing mysql with brew again
brew install mysql
2) upgrading mysql with brew to latest version (if required)
brew upgrade mysql
3) installing mysqlclient now with pip3 (installing globally without virtualenv)
pip3 install mysqlclient
4) now access virtualenv and instal the mysqlclient in it, it will install fine without any error for configparser

As can be seen in the bug report posted by #Alasdair, the solution is:
Set use_pure=True in DATABASES['default']['OPTIONS'].

I too went through this pain yesterday or 2 days ago and was able to get it running with the mysqlclient-1.3.12. I'm going from memory here so bear with me, I tried a lot of things but eventually I got it to work.
I installed mysql8 and mysql8connector from the mysql web site as you did but got no love. After much searching and many trial and errors I found an answer somewhere which I can't find my way back to but I ended up doing:
brew install mysql
pip install mysqlclient
I know that you already have mysql installed but brew install mysql seems to add client libraries that are used to compile the mysqlclient connector. Then my database in my settings file looks like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'polls',
'USER': 'myuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
'OPTIONS': {
'charset': 'utf8mb4',
'autocommit': True,
},
}
}
Note the ENGINE is different. I can replicate your exact error if I change my ENGINE to mysql.connector.django.
However, using django.db.backends.mysql I still get the following warning:
lib/python3.6/site-packages/django/db/backends/mysql/base.py:71: Warning: (3719, "'utf8' is currently an alias for the character set UTF8MB3, which will be replaced by UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous."
which I can't figure out, but as it is only a warning the Django tutorial seems to be working fine.
Let me know if this helps or if you have other questions and I'll do my best to help.

This is mysql-connector-python data conversion issue. You can get around this by setting the database options parameter with use_pure=true. This issue is not fixed in the current version 8.0.11,8.0.12, and 8.0.13.
"OPTIONS":{
"use_pure":True
}
This is just a temporary solution. Refer here for more information: https://bugs.mysql.com/bug.php?id=90541

Related

Airflow gives error ImportError: No module named MySQLdb upon changing its config file for celery

I have installed airflow with celery in my ec2 linux instance and airflow is working like a charm. Now when I made changes in its config file to use celery with airflow it is throwing the following error.
[2020-04-03 07:24:57,234] {settings.py:253} INFO - settings.configure_orm(): Using pool settings. pool_size=5, max_overflow=10, pool_recycle=1800, pid=4851
Traceback (most recent call last):
File "/usr/local/bin/airflow", line 25, in <module>
from airflow.configuration import conf
File "/usr/local/lib/python2.7/site-packages/airflow/__init__.py", line 47, in <module>
settings.initialize()
File "/usr/local/lib/python2.7/site-packages/airflow/settings.py", line 377, in initialize
configure_orm()
File "/usr/local/lib/python2.7/site-packages/airflow/settings.py", line 266, in configure_orm
engine = create_engine(SQL_ALCHEMY_CONN, **engine_args)
File "/usr/local/lib64/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 479, in create_engine
return strategy.create(*args, **kwargs)
File "/usr/local/lib64/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 87, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/usr/local/lib64/python2.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 118, in dbapi
return __import__("MySQLdb")
ImportError: No module named MySQLdb
pip install MySQL-python
Should fix this

Multiple errors while connecting to a database - python3.7

whenever I execute the following mysqlclient commands in python(3.7.4), a bunch of errors show up.
import MySQLdb
import mysql.connector
db = MySQLdb.connect(host='localhost',
port='3306',
database='py_test',
user='arnav',
password='1234')
db_cur = db.cursor()
db_cur.execute('insert into pass values ( \'arnav\', \'0000\' )')
These are the errors and I can't seem to find what the problem is. And my server is running just fine.
errors :
Traceback (most recent call last):
File "C:\Users\arnav\Desktop\python programs\sql.py", line 8, in <module>
password='1234')
File "C:\Users\arnav\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\MySQLdb\__init__.py", line 84, in Connect
return Connection(*args, **kwargs)
File "C:\Users\arnav\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\MySQLdb\connections.py", line 166, in __init__
super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required (got type str)
I have used mysql.connector in the past multiple times and it works perfectly fine, but I need to use mysqlclient for my project. So, please help.
I just used an f-string and it worked.

AttributeError in generating peewee model from mysql using pwiz

When i run pwiz to generate a peewee modem from existing database it shows following error:
root#server:~# python -m pwiz -e mysql -P -H 127.0.0.1 mysql
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/local/lib/python2.7/dist-packages/pwiz.py", line 202, in <module>
print_models(introspector, tables, preserve_order=options.preserve_order)
File "/usr/local/lib/python2.7/dist-packages/pwiz.py", line 47, in print_models
database = introspector.introspect(table_names=tables)
File "/usr/local/lib/python2.7/dist-packages/playhouse/reflection.py", line 440, in introspect
tables = self.metadata.database.get_tables()
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3089, in get_tables
return [table for table, in self.execute_sql('SHOW TABLES')]
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 2459, in execute_sql
cursor = self.cursor(commit)
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 2445, in cursor
self.connect()
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 2411, in connect
self._state.set_connection(self._connect())
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3083, in _connect
return mysql.connect(db=self.database, **self.connect_params)
AttributeError: 'NoneType' object has no attribute 'connect'
What is the problem? The username and password of database is correct.
PyMySql is not installed. Install it using pip or other available options:
pip install PyMySql
Unfortunately, peewee does not write `PyMySql1 module of python in its dependency list, so it should be installed separately.

Flask_SQLAlchemy, MySQL, store Swedish characters å, ä, ö?

I can't sto re Swedish characters in MySQL by using Flask_SQLAlchemy :( I have tried to find a solution for a week now and I really need help as it feels like I have reached a dead end. I think it could be something wrong with the version compatibilities abong my tools, but I don't hope so! I am trying to build an website using Flask, Flask_SQLAlchemy and MySQL (5.5.3). If this is unsolvable, I am considering changing Flask_SQLAlchemy to something else..
(I have taken one course in programming (Python) and the rest is self thought so I would be very happy if you could be as detailed as possible in your answers, very thankful for any advice as well!)
(competeEnv) C:\>conda list
# packages in environment at C:\Users\MyName\Anaconda3.1\envs\competeEnv:
#
click 6.6 py27_0
flask 0.11.1 py27_0
Flask-SQLAlchemy 2.1 <pip>
itsdangerous 0.24 py27_0
jinja2 2.8 py27_1
markupsafe 0.23 py27_2
mysql-python 1.2.5 py27_0
pip 9.0.1 py27_0
python 2.7.12 0
setuptools 27.2.0 py27_1
sqlalchemy 1.1.4 py27_0
vs2008_runtime 9.00.30729.1 2
werkzeug 0.11.11 py27_0
wheel 0.29.0 py27_0
(competeEnv) C:\>
Here is the code in testAlchemy.py file
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:myPassword#myServer/firstdb'
app.config['SQLALCHEMY_ECHO'] = False
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=True
app.config['MYSQL_DATABASE_CHARSET'] = 'utf8mb4'
db = SQLAlchemy(app)
class Users(db.Model):
__tablename__='users'
id=db.Column('iduser', db.Integer, primary_key=True)
name=db.Column('column_name', db.String(193))
def __init__(self, name):
self.name=name
def __repr__(self):
return self.name
db.create_all()
db.session.commit()
president1=Users('Obama')
president2=Users('Trump')
db.session.add(president1)
db.session.add(president2)
db.session.commit()
Here should be some hints..
(competeEnv) C:\Users\MyName\Anaconda3.1\envs>python
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jun 29 2016, 11:07:13)[MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> from testAlchemy import Users, db
>>> db.session.add(Users('Federer'))
>>> db.session.commit()
>>> Users.query.all()
[Obama, Trump, Federer]
>>> db.session.add(Users('ä'))
>>> db.session.commit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\orm\scoping.py", line 157, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\orm\session.py", line 801, in commit
self.transaction.commit()
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\orm\session.py", line 392, in commit
self._prepare_impl()
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\orm\session.py", line 372, in _prepare_impl
self.session.flush()
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\orm\session.py", line 2019, in flush
self._flush(objects)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\orm\session.py", line 2137, in _flush
transaction.rollback(_capture_exception=True)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\orm\session.py", line 2101, in _flush
flush_context.execute()
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 373, in execute
rec.execute(self)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 532, in execute
uow
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\orm\persistence.py", line 174, in save_obj
mapper, table, insert)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\orm\persistence.py", line 800, in _emit_insert_statements
execute(statement, params)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\engine\base.py", line 914, in execute
return meth(self, multiparams, params)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\sql\elements.py", line 323, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\engine\base.py", line 1010, in _execute_clauseelement
compiled_sql, distilled_params
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\engine\base.py", line 1146, in _execute_context
context)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\engine\base.py", line 1341, in _handle_dbapi_exception
exc_info
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\util\compat.py", line 202, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\engine\base.py", line 1139, in _execute_context
context)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\sqlalchemy\engine\default.py", line 450, in do_execute
cursor.execute(statement, parameters)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "C:\Users\MyName\Anaconda3.1\envs\competeEnv\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1366, "Incorrect string value: '\\x84' for column 'column_name' at row 1") [SQL: u'INSERT INTO users (column_name) VALUES (%s)'] [parameters: ('\x84',)]
>>>
Since you're using Python 2.7, you need to specify that your string contains unicode.
>>> db.session.add(Users(u'ä'))
You can also use a future import to treat all strings as unicode.
from __future__ import unicode_literals
Alternatively you can upgrade your version of Python. 2.7 is the last version to treat strings as bytes rather than unicode.
Edit
You'll also need to update your __repr__ so that it properly handles unicode.
def __repr__(self):
return self.name.decode('utf-8')
or whatever encoding you want to use.
In general, you'll need to make sure you handle encoding from and decoding to unicode. I can't urge you enough to consider using a more recent version of Python. One of the largest changes in Python 3 addresses this very issue.
In using UTF-8 characters, try to avoid any kind of encode/decode; that just masks the real problem, which is usually in the configuration somewhere.
My notes on sqlalchemy:
db_url = sqlalchemy.engine.url.URL(drivername='mysql', host=foo.db_host,
database=db_schema,
query={ 'read_default_file' : foo.db_config, 'charset': 'utf8' })
json.dumps(mydict, ensure_ascii=False) avoids "\u...." strings.
https://docs.sqlalchemy.org/en/latest/dialects/mysql.html#mysql-unicode
Python 2.7 is rather old, see this about the differences with 3:
https://stackoverflow.com/a/40708131/1766831
Python tips for utf8: http://mysql.rjweb.org/doc.php/charcoll#python

django-balanced - Database Error

I'm trying to install the django-balanced app in my project and I'm getting an error.
I did the following as the readme file says:
#Add to settings
import os
BALANCED = {
'API_KEY': os.environ.get('BALANCED_API_KEY'),
}
INSTALLED_APPS = (
...
'django.contrib.admin', # if you want to use the admin interface
'django_balanced',
...
)
#ran the following on my project root folder
BALANCED_API_KEY=YOUR_API_KEY django-admin.py syncdb #replacing the YOUR_API_KEY with my key
When I do this, I get the following error:
Traceback (most recent call last):
File "/usr/local/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/core/management/commands/syncdb.py", line 8, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/Library/Python/2.7/site-packages/django/core/management/sql.py", line 9, in <module>
from django.db import models
File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 11, in <module>
if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 46, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
When I do the python manage.py syncdb, I get no errors with the database. I might be missing something in the database setting that the readme file does not mention. Thanks in advance
here is my database settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'payload', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'pass123', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
You can also type the following command
BALANCED_API_KEY=YOUR_API_KEY python manage.py syncdb
You need to set the DJANGO_SETTINGS_MODULE:
You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings