Django 根据数据模型models创建数据表的实例
作者:bianruifeng的专栏 时间:2021-08-25 18:13:31
如果使用默认的数据库 SQLite3,则无需配置settings.py
使用其他数据库,则需要配置settings.py,这里以Mysql为例;
DATABASES = {
'default': {
'ENGINE':'django.db.backends.mysql',
'NAME':'webapp',#数据库名
'USER':'test1',#用户名
'PASSWORD':'123456',#密码
'HOST':'127.0.0.1',
'PORT':'3306',
}
}
在models.py中完成数据模型的创建:
class student(models.Model):
# class Meta:
# db_table = 'User_table'#指定数据表的名称
name = models.CharField(max_length=50)
sex = models.CharField(max_length=10)
birthday = models.DateField()
telephone = models.BigIntegerField()
def __str__(self):
return self.name
并且在admin.py中完成注册:
from webapp import models
admin.site.register(models.student)
最后 cd 进入 manage.py 所在的那个文件夹下,输入下面的命令
# Django 1.6.x 及以下
python manage.py syncdb
# Django 1.7 及以上的版本需要用以下命令
python manage.py makemigrations
python manage.py migrate
#python2.7 manage.py **** #如果安装多个python版本的话,最好指定版本
如果执行python manage.py migrate报错,尝试python2.7 manage.py migrate appname --fake命令。
如果还不行,检查有没有错误,再来一遍。
来源:https://blog.csdn.net/bianruifeng/article/details/75008518
标签:Django,models,创建,数据表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Oracle教程 误添加数据文件删除方法
2009-08-04 12:38:00
python实现转盘效果 python实现轮盘抽奖游戏
2023-03-06 00:00:57
隐藏你的.php文件的实现方法
2023-10-20 22:58:01
Python使用progressbar模块实现的显示进度条功能
2023-11-20 05:40:07
python中watchdog文件监控与检测上传功能
2022-03-17 13:46:58
Python和Bash结合在一起的方法
2023-11-02 20:53:27
在ORACLE移动数据库文件
2010-08-02 12:54:00
python字符串常用方法
2023-05-29 13:54:15
Python实现为图像添加下雪特效
2021-07-10 11:28:45
![](https://img.aspxhome.com/file/2023/9/97369_0s.gif)
python图片处理库Pillow实现简单PS功能
2021-08-29 13:51:23
![](https://img.aspxhome.com/file/2023/0/78550_0s.jpg)
Python基于pillow库实现生成图片水印
2021-08-01 10:45:38
解决用CSS控制DIV居中失效的问题
2010-04-05 21:53:00
如何设计广告的用户体验?
2007-12-20 13:12:00
![](https://img.aspxhome.com/file/UploadPic/200712/20/20071220131356270s.jpg)
PHP中合并数组的常见方法分享
2023-05-25 12:02:48
Python中使用支持向量机SVM实践
2022-03-17 16:14:21
PHP simplexml_load_file()函数讲解
2023-06-03 23:16:56
![](https://img.aspxhome.com/file/2023/2/55402_0s.png)
asp防止同时登陆的问题
2007-10-26 12:19:00
聊聊python中的循环遍历
2022-06-17 23:44:43
你的网站使用了微格式了么
2009-05-21 12:10:00
详解MySQL数据库中Show命令的用法
2008-11-27 16:04:00