python: 3.6.4
django: 2.0
models.py 代码如下
# coding: utf-8 from django.db import models from django.contrib.auth.models import User # Create your models here. class Category(models.Model): name = models.CharField(max_length=100) class Tag(models.Model): name = models.CharField(max_length=100) class Post(models.Model): title = models.CharField(max_length=70) body = models.TextField() created_time = models.DateTimeField() modified_time = models.DateTimeField() excerpt = models.CharField(max_length=200, blank=True) category = models.ForeignKey(Category,on_delete=models.CASCADE) tags = models.ManyToManyField(Tag, blank=True) author = models.ForeignKey(User,on_delete=models.CASCADE)
错误信息:
解决办法:
外键值的后面加上 on_delete=models.CASCADE
category = models.ForeignKey(Category)
author = models.ForeignKey(User)
改为
category = models.ForeignKey(Category,on_delete=models.CASCADE)
author = models.ForeignKey(User,on_delete=models.CASCADE)
再次执行 ,成功