python - Django - manage.py (python managepy makemigrations )错误

我是python和Django的新手,跟随一个教程,Vitor Freitas链接:https://simpleisbetterthancomplex.com/series/2017/09/11/a-complete-beginners-guide-to-django-part-2.html

我把这个代码复制到models.py


from django.db import models


from django.contrib.auth.models import User



# Create your models here.


class Board(models.Model):


 name = models.CharField(max_length=30, unique=True)


 description = models.CharField(max_length=100)



class Topic(models.Model):


 subject = models.CharField(max_length=255)


 last_updated = models.DateTimeField(auto_now_add=True)


 board = models.ForeignKey(Board, related_name='topics')


 starter = models.ForeignKey(User, related_name='topics')



class Post(models.Model):


 message = models.TextField(max_length=4000)


 topic = models.ForeignKey(Topic, related_name='posts')


 created_at = models.DateTimeField(auto_now_add=True)


 updated_at = models.DateTimeField(null=True)


 created_by = models.ForeignKey(User, related_name='posts')


 updated_by = models.ForeignKey(User, null=True, related_name='+')



当我在cmd manage.py上运行python manage.py makemigrations

收到这个错误


Traceback (most recent call last):


 File"manage.py", line 21, in <module>


 main()


 File"manage.py", line 17, in main


 execute_from_command_line(sys.argv)


 File"C:Usersadmindevelopmentmyprojectenvlibsite-packagesdjangocoremanagement__init__.py", line 401, in execute_from_command_line


 utility.execute()


 File"C:Usersadmindevelopmentmyprojectenvlibsite-packagesdjangocoremanagement__init__.py", line 377, in execute


 django.setup()


 File"C:Usersadmindevelopmentmyprojectenvlibsite-packagesdjango__init__.py", line 24, in setup


 apps.populate(settings.INSTALLED_APPS)


 File"C:Usersadmindevelopmentmyprojectenvlibsite-packagesdjangoappsregistry.py", line 114, in populate


 app_config.import_models()


 File"C:Usersadmindevelopmentmyprojectenvlibsite-packagesdjangoappsconfig.py", line 211, in import_models


 self.models_module = import_module(models_module_name)


 File"C:Usersadmindevelopmentmyprojectenvlibimportlib__init__.py", line 127, in import_module


 return _bootstrap._gcd_import(name[level:], package, level)


 File"<frozen importlib._bootstrap>", line 1014, in _gcd_import


 File"<frozen importlib._bootstrap>", line 991, in _find_and_load


 File"<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked


 File"<frozen importlib._bootstrap>", line 671, in _load_unlocked


 File"<frozen importlib._bootstrap_external>", line 783, in exec_module


 File"<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed


 File"C:Usersadmindevelopmentmyprojectmyprojectboardsmodels.py", line 12, in <module>


 class Topic(models.Model):


 File"C:Usersadmindevelopmentmyprojectmyprojectboardsmodels.py", line 15, in Topic


 board = models.ForeignKey(Board, related_name='topics')


TypeError: __init__() missing 1 required positional argument: 'on_delete'



我无法解决该错误。

时间: 作者:

有多个选项,但是最安全的方法是使用models.PROTECT

解决方案


 models.ForeignKey(Board, on_delete=models.PROTECT)



https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ForeignKey.on_delete

作者:

topic = models.ForeignKey(Topic, related_name='posts', on_delete=models.CASCADE)



在删除引用对象时,必须添加删除行为,引用https://docs.djangoproject.com/en/3.0/ref/models/fields/#django.db.models.ForeignKey.on_delete

作者:
...