Search code examples
pythondjangoworkflow

Django 1 to Django 2 on_delete error


I believe this workflow was created for a previous Django version. Now when I am trying to upgrade it I get an error to add on_delete. Here is what I have done but it is still not working and I'm wondering what I am doing wrong.

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)

Then I get another error saying the model is not defined.


Solution

  • According to the Django 2.0 docs (and also the release notes) all Foreignkey fields now have a required on_delete parameter.

    It seems to be missing on your model's fields. The release notes also advise to take a look at your migrations:

    The on_delete argument for ForeignKey and OneToOneField is now required in models and migrations. Consider squashing migrations so that you have fewer of them to update.