Django 1到Django 2 on_delete错误

时间:2018-06-09 15:59:46

标签: python django workflow

我相信这个工作流程是为以前的Django版本创建的。现在,当我尝试升级它时,我收到错误添加on_delete。这是我所做的,但它仍然没有用,我想知道我做错了什么。

ORIGINAL

class Task(AbstractEntity):
    request = ForeignKey(Request, related_name='tasks')   
    assignee = ForeignKey(Group)    
    updated_by = ForeignKey(User)    
    activity_ref = CharField(max_length=100)    
    status = CharField(verbose_name="Status", max_length=30, choices=TASK_STATUS)

MY VERSION

class Task(models.AbstractEntity):
    request = models.ForeignKey(Request, related_name='tasks', on_delete=models.CASCADE)
    assignee = ForeignKey(Group)
    updated_by = ForeignKey(User)
    activity_ref = CharField(max_length=100)
    status = CharField(verbose_name="Status", max_length=30, choices=TASK_STATUS)

然后我得到另一个错误,说明模型没有定义。

2 个答案:

答案 0 :(得分:0)

根据Django 2.0 docs(以及release notes),所有Foreignkey字段现在都有一个必需的on_delete参数。

你的模特领域似乎缺少了。发行说明还建议您查看迁移:

  

现在,模型和迁移中需要ForeignKey和OneToOneField的on_delete参数。考虑压缩迁移,以便更新更少。

答案 1 :(得分:0)

有一个解决方法。

如果有很多表需要在使用外键或一对一键的地方添加“ on_delete”参数,那么遍历所有模型来添加on_delete参数将非常麻烦。 您可以执行外键的猴子补丁来实现您的目标。

在您的项目设置文件夹中添加以下猴子补丁文件:

monkey_patches.py

from django.db import models
from django.db.models import ForeignKey
from django.db import models
from django.db.models import ForeignKey, OneToOneRel
from django.db.models.fields.related_descriptors import ReverseOneToOneDescriptor, ForwardOneToOneDescriptor
from django.utils.translation import gettext_lazy as _


class ForeignKeyMonkeyPatch(ForeignKey):
    def __init__(self, to, on_delete=None, *args, **kwargs):
        super().__init__(to, on_delete, *args, **kwargs)


class OneToOneFieldMonkeyPatch(ForeignKeyMonkeyPatch):
    many_to_many = False
    many_to_one = False
    one_to_many = False
    one_to_one = True

    related_accessor_class = ReverseOneToOneDescriptor
    forward_related_accessor_class = ForwardOneToOneDescriptor
    rel_class = OneToOneRel

    description = _("One-to-one relationship")

    def __init__(self, to, on_delete=None, *args, **kwargs):
        kwargs['unique'] = True
        super().__init__(to, on_delete, *args, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        if "unique" in kwargs:
            del kwargs['unique']
        return name, path, args, kwargs

    def formfield(self, **kwargs):
        if self.remote_field.parent_link:
            return None
        return super().formfield(**kwargs)

    def save_form_data(self, instance, data):
        if isinstance(data, self.remote_field.model):
            setattr(instance, self.name, data)
        else:
            setattr(instance, self.attname, data)

    def _check_unique(self, **kwargs):
        # Override ForeignKey since check isn't applicable here.
        return []




setattr(models, 'ForeignKey', ForeignKeyMonkeyPatch)
setattr(models, 'OneToOneField', OneToOneFieldMonkeyPatch)

现在,在您的项目设置中调用此文件...

从.monkey_patches导入*

上面的代码将使用我们的猴子修补类重置 ForeignKey OneToOneField ,从而将 on_delete 设置为 None strong>默认情况下

P.S。 (您甚至可以创建一个应用程序(名为“ monkey_patches”或您想要的任何东西。。),并将以上代码添加到models.py中)