天天看點

Django開發問題及解決方法彙總

1.

Django開發問題及解決方法彙總

manage.py@MxOnline > makemigrations users

manage.py@MxOnline > migrate users      

 2.

操作django的admin,添加使用者時報錯:
1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_log`, CONSTRAINT `django_admin_log_user_id_c564eba6_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))')      

解決方法:

在settings.py中添加databases的參數

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': "mxonline",
        "USER": "root",
        "PASSWORD": "redhat",
        "HOST": "127.0.0.1",
        #解決admin的外鍵報錯
        "OPTIONS": {
            "init_command": "SET foreign_key_checks=0;",
        }
    }
}      

 3.python3  xadmin在python3配置過程出現

NameError: name 'reload' is not defined報錯

解決辦法:

不能pip install xadmin

需要在https://github.com/sshwsfc/xadmin網址這裡下載下傳xadmin

Field 'id' doesn't have a default value 原因

Field 'id' doesn't have a default value昨晚做項目的時候遇到一個問題,在測試資料存儲的時候老是報Field 'id' doesn't have a default value異常,從網上找了好久,根據各位大蝦的說法也測試了好久好久,可就是沒發現原因所在,鼓搗了兩三個小時的時間,最後總算找到問題所在:原來是我的資料設計的時候,把主鍵的類型定義為int的,原本想是用自增的方式來的,可是由于自己的粗心,寫sql語句的時候沒有加上auto_increment,是以在資料存儲的時候老是報Field 'id' doesn't have a default value,id根本就沒有值啊!!
加上自己從網上找的其他人說的他們遇到這種時候的原因,在這裡總結一下:
1、打開my.ini,查找
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

修改為
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

然後重新開機MYSQL

2、MySQL 5 uses a strict mode which needs to be disabled.
In Windows, Goto Start-->Programs-->MySQL->MySQL Instance Config Wizard. Follow through the Reconfigure Instance option-->Detailed Configuration-->Continue Next a few screens. At the bottom under Enable TCP/IP option there is 'Enable Strict Mode'. Deslect this option (no tick). Save changes and MySQL will restart.

3、看看你的資料庫定義的時候是不是把主鍵生成方式設定為int的,但是沒有設定為自增的!!或者資料定義的時候設定一個預設值就可以了。      

繼續閱讀