Django踩坑记录1

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)                    #关联发布会
    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

报错如下:

解决办法:

将代码改为:

event = models.ForeignKey(Event,on_delete=models.CASCADE,)即在外键值的后面加上 on_delete=models.CASCADE

原因分析:

在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:
TypeError: __init__() missing 1 required positional argument: 'on_delete'

 
posted @ 2020-06-20 10:22  Harry_666  阅读(272)  评论(0编辑  收藏  举报