在前面我们已经学习了如果创建一个网站。不过没有多少功能,并且没有对数据库做设计。
django是使用python语言对象关系映射来设计数据库布局
方法/步骤
-
数据模型语法提供丰富的表现模型的方法。代码如下:
from django.db import models
# Create your models here.
class Reporter(models.Model):
full_name = models.CharField(max_length=70)
def __str__(self):
return self.full_name
class Article(models.Model):
pub_date = models.DateField()
headline = models.CharField(max_length = 200)
content = models.TextField()
reporter = models.ForeignKey(Reporter)
def __str__(self):
return self.headline
-
接下来,运行Django命令自动创建数据为表格
python3 manage.py migrate
migrate命令着眼于所有有效models并创建不存在的数据表格。
1万+

被折叠的 条评论
为什么被折叠?



