修改models.py文件:
# models.py
from django.db import models
class Test(models.Model):
name = models.CharField(max_length=20)
class Contact(models.Model):
name = models.CharField(max_length=200)
age = models.IntegerField(default=0)
email = models.EmailField()
def __unicode__(self):
return self.name
class Tag(models.Model):
#contact = models.ForeignKey(Contact)
contact = models.ForeignKey('Contact', on_delete=models.CASCADE)
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
修改admin.py文件:
from django.contrib import admin
from TestModel.models import Test,Contact,Tag
# Register your models here.
admin.site.register([Test,Contact,Tag])
刷新浏览器,Testmodel 数据模型 中添加了 新的数据表

但是点击Tags和Contacts进入下级页,报错
解决方法,执行以下步骤,将表创建到mysql中
python3 manage.py makemigrations TestModel # 让 Django 知道我们在我们的模型有一些变更
python3 manage.py migrate TestModel # 创建表结构

再点进去就正常了

网友评论