django常见错误

一、django foreign key 自动加_id问题
例如:uid = models.ForeignKey(AccountsModels,related_name="fx_base_uid",db_column="uid")
如果不写db_column,最后生成的会是uid_id

二、外键问题
报错为:
bases.BasesModels.uid: (fields.E302) Reverse accessor for 'BasesModels.uid' clashes with field name 'AccountsModels.uid'.
HINT: Rename field 'AccountsModels.uid', or add/change a related_name argument to the definition for field 'BasesModels.uid'.
bases.BasesModels.uid: (fields.E303) Reverse query name for 'BasesModels.uid' clashes with field name 'AccountsModels.uid'.
HINT: Rename field 'AccountsModels.uid', or add/change a related_name argument to the definition for field 'BasesModels.uid'.
解决方法为更改related_name="uid"为related_name="fx_base_uid",出错是因为外键名字冲突了

三、django打印sql语句
在settings.py里面加上
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console':{
'level':'DEBUG',
'class':'logging.StreamHandler',
},
},
'loggers': {
'django.db.backends': {
'handlers': ['console'],
'propagate': True,
'level':'DEBUG',
},
}
}

三、pip安装mysql错误
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build-gpmw8pju/mysqlclient/
在ubuntu里面
sudo apt-get install python-dev python3-dev
sudo apt-get install libmysqlclient-dev
pip install pymysql
pip install mysqlclient
在centos里面
yum install mysql-devel
yum install MySQL-python
之后错误消失,所有依赖能正确安装上。

标签: none

添加新评论