__init__() missing 1 required positional argument: 'on_delete'

学习django模型时,创建了两个数据库表。第二个引用第一个表的id作为外键时,报了一个 __init__() missing 1 required positional argument: 'on_delete'的错误。

__init__() missing 1 required positional argument: 'on_delete'_第1张图片

百思不得其解,然后查询资料发现需要一个on_delete参数

于是,从善如流的加了一个


于是,在cmd命令里面输入python manage.py makemigrations sign

问题解决

__init__() missing 1 required positional argument: 'on_delete'_第2张图片

完整model代码:

from django.db import models

# Create your models here.


class Event(models.Model):
    name = models.CharField(max_length=100)
    limit = models.IntegerField()
    status = models.BooleanField()
    address = models.CharField(max_length=200)
    start_time = models.DateTimeField('events time')
    create_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name


class Guest(models.Model):
    event = models.ForeignKey(Event,on_delete=models.DO_NOTHING)
    realname = models.CharField(max_length=64)
    phone = models.CharField(max_length=16)
    email = models.EmailField()
    sign = models.BooleanField()
    create_time = models.DateTimeField(auto_now=True)

    class Meta:
        unique_together = ('event', 'phone')

    def __str__(self):
        return self.realname


你可能感兴趣的:(__init__() missing 1 required positional argument: 'on_delete')