gpt4 book ai didi

python - 类型错误 : on_delete must be callable

转载 作者:行者123 更新时间:2023-12-04 07:49:58 26 4
gpt4 key购买 nike

File "C:\Users\sungk\Git\django_website\blog\models.py", line 5, in <module>     class Post(models.Model):
File "C:\Users\sungk\Git\django_website\blog\models.py", line 10, in Post author = models.ForeignKey(User, on_delete=True) File "C:\Users\sungk\Git\django_website\venv\lib\site-packages\django\db\models\fields\related.py", line 813, in __init__ raise TypeError('on_delete must be callable.') TypeError: on_delete must be callable.

最佳答案

on_delete=… parameter [Django-doc]不能True .它应该是一个可调用的,一个函数。通常它是内置函数之一,如 described in the documentation .这可以是 CASCADE , PROTECT , RESTRICT , SET_NULL , SET_DEFAULT , SET(…) , 或 DO_NOTHING .
严格来说,您也可以使自己的可调用,但这仅适用于您想做比上面列出的更复杂的事情。on_delete=…指定如何处理相关 Post以防万一User删除作者的对象。通过使用 CASDCADE ,相关Post对象将被删除:

from django.conf import settings
from django.db import models

class Post(models.Model):
author = models.FOreignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE
)

Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

关于python - 类型错误 : on_delete must be callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67034783/

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