django自带ORM功能,继承django的model类就可以创建类似于hibernate的entity,django默认使用的是sqlnite数据库,该数据库主要用于移动端
安装sqllite客户端就可以打开该数据库
apt install sqliteman
sqliteman 数据命令打开数据库
步骤:
1 、继承Model类
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=20)
create_date = models.DateField()
2、生成迁移文件,迁移文件就是把对象的属性放到一个文件里用于生成数据库表
生成迁移文件之前
进入项目根目录
python manage.py makemigrations
生成迁移文件如下图
this command will be show migration process
python manage.py sqlmigrate modelname 0001
you should see something similar to the following
3、生成数据库表
python manage.py migrate
Change your models (in models.py).
Run python manage.py makemigrations to create migrations for those changes
Run python manage.py migrate to apply those changes to the database.
try to api, we can operator database using django api directly
first of all we perform follow command at the root directory of django project
python manage.py shell
<QuerySet [<Stock: Stock object (1)>]> isn't a helpful representation,let's fix that by editing Stock model and adding a _str_ method to it.
Django Admin
creating an admin user