- 1 min

Use one-to-one relationships in Django2.0 models

报错信息

 File "/Users/topaz/Desktop/Project/PWO/models.py", line 110, in UserProfile
 user = models.OneToOneField(User)
 TypeError: __init__() missing 1 required positional argument: 'on_delete'

解决

 1.找到报错行
 user = models.OneToOneField(User)

 2.添加 models.CASCADE
 user = models.OneToOneField(User,models.CASCADE)

原因:

 之前用的 django1.8 版本,升级后的2.0版本发生变化,需要设置 'on_delete'= models.CASCADE 才能正常使用。
Topaz

Topaz

Always keep learning.

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin google pinterest medium vimeo