gpt4 book ai didi

python - 获取类型错误 : __init__() missing 1 required positional argument: 'on_delete' when trying to add parent table after child table with entries

转载 作者:IT老高 更新时间:2023-10-28 21:35:54 25 4
gpt4 key购买 nike

我的 sqlite 数据库中有两个类,一个名为 Categorie 的父表和名为 Article 的子表.我首先创建了子表类并添加了条目。所以首先我有这个:

class Article(models.Model):
titre=models.CharField(max_length=100)
auteur=models.CharField(max_length=42)
contenu=models.TextField(null=True)
date=models.DateTimeField(
auto_now_add=True,
auto_now=False,
verbose_name="Date de parution"
)

def __str__(self):
return self.titre

在我添加了父表之后,现在我的 models.py看起来像这样:

from django.db import models

# Create your models here.
class Categorie(models.Model):
nom = models.CharField(max_length=30)

def __str__(self):
return self.nom


class Article(models.Model):
titre=models.CharField(max_length=100)
auteur=models.CharField(max_length=42)
contenu=models.TextField(null=True)
date=models.DateTimeField(
auto_now_add=True,
auto_now=False,
verbose_name="Date de parution"
)
categorie = models.ForeignKey('Categorie')

def __str__(self):
return self.titre

所以当我运行 python manage.py makemigrations <my_app_name> ,我得到这个错误:

Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-2.0-py3.5.egg\django\core\management\__init__.py", line 354, in execute_from_command_line
utility.execute()
File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-2.0-py3.5.egg\django\core\management\__init__.py", line 330, in execute
django.setup()
File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-2.0-py3.5.egg\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-2.0-py3.5.egg\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-2.0-py3.5.egg\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "C:\Users\lislis\Django\mon_site\blog\models.py", line 6, in <module>
class Article(models.Model):
File "C:\Users\lislis\Django\mon_site\blog\models.py", line 16, in Article
categorie = models.ForeignKey('Categorie')
TypeError: __init__() missing 1 required positional argument: 'on_delete'

我在stackoverflow中看到了一些类似的问题,但似乎不是同一个问题:__init__() missing 1 required positional argument: 'quantity'

最佳答案

你可以像这样改变Article类的categorie属性:

categorie = models.ForeignKey(
'Categorie',
on_delete=models.CASCADE,
)

错误应该会消失。

最终,您可能需要 on_delete 的另一个选项,查看文档了解更多详细信息:

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

编辑:

正如您在评论中所说,您对 on_delete 没有任何特殊要求,您可以使用选项 DO_NOTHING:

# ...
on_delete=models.DO_NOTHING,
# ...

关于python - 获取类型错误 : __init__() missing 1 required positional argument: 'on_delete' when trying to add parent table after child table with entries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44026548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com