我在django项目[2.0.8]项目中使用django-podcasting。
运行python manage.py makemigrations
时,得到以下堆栈跟踪:
(site-env) me@YOURBOX:~/Projects/dev/mysite$ python base_app/manage.py makemigrations
Traceback (most recent call last):
File "base_app/manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/path/to/proj/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_comman
d_line
utility.execute()
File "/path/to/proj/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/path/to/proj/env/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/path/to/proj/env/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/path/to/proj/env/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/path/to/proj/env/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/path/to/proj/env/lib/python3.6/site-packages/podcasting/models.py", line 103, in <module>
class Show(models.Model):
File "/path/to/proj/env/lib/python3.6/site-packages/podcasting/models.py", line 128, in Show
help_text=_("""Make certain the user account has a name and e-mail address."""))
TypeError: __init__() missing 1 required positional argument: 'on_delete'
答案 0 :(得分:0)
在新版本的Django中,on_delete
和ForeignKey
必须使用OneToOneField
。
您必须在模型中对其进行定义,例如:
class Car(models.Model):
manufacturer = models.ForeignKey(
'Manufacturer',
on_delete=models.CASCADE,
)
以下是文档中on_delete的不同可能性:https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.ForeignKey.on_delete
on_delete
不在数据库中创建SQL约束。对数据库级级联选项的支持可能会在以后实现。
on_delete的可能值在django.db.models中找到:
CASCADE
级联删除。 Django会在DELETE CASCADE上模拟SQL约束的行为,并删除包含ForeignKey的对象。
不会在相关模型上调用Model.delete(),但会为所有已删除对象发送pre_delete和post_delete信号。
保护
通过引发ProtectedError(django.db.IntegrityError的子类)来防止引用对象的删除。
SET_NULL
将ForeignKey设置为null;仅当null为True时才可能。
SET_DEFAULT
将ForeignKey设置为其默认值;必须设置ForeignKey的默认值。
SET()
将ForeignKey设置为传递给SET()的值,或者如果传递了可调用对象,则为调用它的结果。在大多数情况下,有必要传递一个callable以避免在导入models.py时执行查询:
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models
def get_sentinel_user():
return get_user_model().objects.get_or_create(username='deleted')[0]
class MyModel(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.SET(get_sentinel_user),
)
DO_NOTHING
Take no action. If your database backend enforces referential integrity, this will cause an IntegrityError unless you manually add an SQL ON DELETE constraint to the database field.