Python3 Django2 编译时的几个报错解决

1.Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: ‘on_delete’

File "E:\PycharmProjects\MxShop\apps\trade\models.py", line 15, in ShoppingCart
    user = models.ForeignKey(User, verbose_name=u"用户")
TypeError: __init__() missing 1 required positional argument: 'on_delete'

解决:

即在外键值的后面加上 on_delete=models.CASCADE

user = models.ForeignKey(User, verbose_name=u"用户", on_delete=models.CASCADE)

2.Django继承AbstractUser新建User Model时出现fields.E304错误

manage.py@MxShop > makemigrations
"E:\Program Files\JetBrains\PyCharm 2017.3.1\bin\runnerw.exe" "C:\Documents and Settings\Administrator\Envs\VueShop\Scripts\python.exe" "E:\Program Files\JetBrains\PyCharm 2017.3.1\helpers\pycharm\django_manage.py" makemigrations E:/PycharmProjects/MxShop
SystemCheckError: System check identified some issues:

ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserProfile.groups'.
	HINT: Add or change a related_name argument to the definition for 'User.groups' or 'UserProfile.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'UserProfile.user_permissions'.
	HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'UserProfile.user_permissions'.
users.UserProfile.groups: (fields.E304) Reverse accessor for 'UserProfile.groups' clashes with reverse accessor for 'User.groups'.
	HINT: Add or change a related_name argument to the definition for 'UserProfile.groups' or 'User.groups'.
users.UserProfile.user_permissions: (fields.E304) Reverse accessor for 'UserProfile.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
	HINT: Add or change a related_name argument to the definition for 'UserProfile.user_permissions' or 'User.user_permissions'.

Process finished with exit code 1

解决:

在settings.py中增加:

...
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

AUTH_USER_MODEL = 'users.UserProfile'

...

执行:

manage.py@MxShop > makemigrations
"E:\Program Files\JetBrains\PyCharm 2017.3.1\bin\runnerw.exe" "C:\Documents and Settings\Administrator\Envs\VueShop\Scripts\python.exe" "E:\Program Files\JetBrains\PyCharm 2017.3.1\helpers\pycharm\django_manage.py" makemigrations E:/PycharmProjects/MxShop
Migrations for 'goods':
  apps\goods\migrations\0001_initial.py
    - Create model Banner
    - Create model Goods
    - Create model GoodsCategory
    - Create model GoodsCategoryBrand
    - Create model GoodsImage
    - Create model HotSearchWords
    - Create model IndexAd
    - Add field category to goods
    - Add field goods to banner
Migrations for 'users':
  apps\users\migrations\0001_initial.py
    - Create model UserProfile
    - Create model VerifyCode
Migrations for 'trade':
  apps\trade\migrations\0001_initial.py
    - Create model OrderGoods
    - Create model OrderInfo
    - Create model ShoppingCart
  apps\trade\migrations\0002_auto_20180112_2139.py
    - Add field user to shoppingcart
    - Add field user to orderinfo
    - Add field goods to ordergoods
    - Add field order to ordergoods
    - Alter unique_together for shoppingcart (1 constraint(s))
Migrations for 'user_operation':
  apps\user_operation\migrations\0001_initial.py
    - Create model UserAddress
    - Create model UserFav
    - Create model UserLeavingMessage
  apps\user_operation\migrations\0002_auto_20180112_2139.py
    - Add field user to userleavingmessage
    - Add field goods to userfav
    - Add field user to userfav
    - Add field user to useraddress
    - Alter unique_together for userfav (1 constraint(s))

Following files were affected 
 E:\PycharmProjects\MxShop\apps\users\migrations\0001_initial.py
E:\PycharmProjects\MxShop\apps\trade\migrations\0002_auto_20180112_2139.py
E:\PycharmProjects\MxShop\apps\user_operation\migrations\0001_initial.py
E:\PycharmProjects\MxShop\apps\trade\migrations\0001_initial.py
E:\PycharmProjects\MxShop\apps\user_operation\migrations\0002_auto_20180112_2139.py
E:\PycharmProjects\MxShop\apps\goods\migrations\0001_initial.py
Process finished with exit code 0
manage.py@MxShop > migrate
"E:\Program Files\JetBrains\PyCharm 2017.3.1\bin\runnerw.exe" "C:\Documents and Settings\Administrator\Envs\VueShop\Scripts\python.exe" "E:\Program Files\JetBrains\PyCharm 2017.3.1\helpers\pycharm\django_manage.py" migrate E:/PycharmProjects/MxShop
Operations to perform:
  Apply all migrations: auth, contenttypes, goods, sessions, trade, user_operation, users
Running migrations:
  Applying goods.0001_initial... OK
  Applying users.0001_initial... OK
  Applying trade.0001_initial... OK
  Applying trade.0002_auto_20180112_2139... OK
  Applying user_operation.0001_initial... OK
  Applying user_operation.0002_auto_20180112_2139... OK

Following files were affected
Process finished with exit code 0

3.django2.x报错No module named ‘django.core.urlresolvers’

File "E:\PycharmProjects\MxShop\extra_apps\xadmin\models.py", line 8, in <module>
    from django.core.urlresolvers import NoReverseMatch, reverse
ImportError: No module named 'django.core.urlresolvers'

简单来说,原因就是:django2.0 把原来的 django.core.urlresolvers 包 更改为了 django.urls包,所以我们需要把导入的包都修改一下就可以了。

from django.urls import reverse

后来发现问题实在太多,按教程使用了django 1.11.3的版本,稀奇古怪的问题都不见了。

所以以后一定要注意版本问题。

    原文作者:li li
    原文地址: https://zhuanlan.zhihu.com/p/32892137
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞