我知道Django 2.2的ForeignHey需要"on_delete";参数,并在我的模型中定义了它。尽管如此,我还是得到了错误";类型错误:init((缺少1个必需的位置参数:"on_delete"请告诉我这个错误的解决方法
我使用Python 3.7和Django 2.2我的型号是
from django.db import models
from treebeard.mp_tree import MP_Node
from django_model_to_dict.mixins import ToDictMixin
class Project(ToDictMixin,models.Model):
name=models.CharField(max_length=200)
def __str__(self):
return self.name
class Task(MP_Node, ToDictMixin):
name=models.CharField(max_length=100)
start=models.DateTimeField()
end=models.DateTimeField()
progress=models.PositiveIntegerField(default=0)
custom_class=models.CharField(max_length=20,null=True, blank=True)
project = models.ForeignKey(
Project,
related_name="tasks",
on_delete=models.CASCADE,
)
def __str__(self):
return self.name
错误为
File "/Users/gensan/python/basic3_7/lib/python3.7/site- packages/django_model_to_dict/models.py", line 127, in <module>
class Order(models.Model, ToDictMixin):
File "/Users/gensan/python/basic3_7/lib/python3.7/site-packages/django_model_to_dict/models.py", line 134, in Order
customer = models.ForeignKey(to=Customer, verbose_name=_('customer'), related_name='orders')
TypeError: __init__() missing 1 required positional argument: 'on_delete'
谢谢
您应该查看定义Order类的models.py,而不是该文件或该文件的其他位置