因此,我是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:\Users\admin\development\myproject\env\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\admin\development\myproject\env\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\Users\admin\development\myproject\env\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\admin\development\myproject\env\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\admin\development\myproject\env\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\admin\development\myproject\env\lib\importlib\__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:\Users\admin\development\myproject\myproject\boards\models.py", line 12, in <module>
class Topic(models.Model):
File "C:\Users\admin\development\myproject\myproject\boards\models.py", line 15, in Topic
board = models.ForeignKey(Board, related_name='topics')
TypeError: __init__() missing 1 required positional argument: 'on_delete'
我无法解决该错误。
答案 0 :(得分:0)
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
答案 1 :(得分:0)
好吧,这是我们创建外键时要传递的标志。这对于所有外键列都很有用。有多种选择,但是最安全的方法是使用foreach(var interval in intervals) {
Console.WriteLine(interval.Item1);
Console.WriteLine(interval.Item2);
}
解决方案
models.PROTECT
https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ForeignKey.on_delete