Django 和ORM展示数据

87 阅读2分钟

ORM operations

什么是ORM

ORM是对象映射模型,目的就是以后在Django项目里写有关数据库的代码时,就不用写MySQL的原生语句了。直接通过Pycharm代码来操作数据库里表的增删改查。

ORM的书写位置

直接在models.py中书写

# 表 ---> 类名
# 记录 ---> 对象
# 字段 ---> 属性

创建表

所有的表(类)必须继承 models.Model

from django.db import models  
  
# Create your models here.  
class test(models.Model):  
# id int primary key auto_increment  
id = models.AutoField(primary_key=True)  
  
# username varchar(64)  
username = models.CharField(max_length=64)  
  
# password varchar(64)  
password = models.CharField(max_length=64)

但是这样还没有成功,我们需要做数据迁移。

迁移Migration

迁移的意思是,在数据库中真正的生成表

操作: 在Pycharm的终端里面输入命令python3.10 manage.py makemigrations 她的作用是把数据库的迁移记录保存下来。

你会得到:

Migrations for 'demo0726':
  demo0726/migrations/0001_initial.py
    - Create model test

上面所创建的表会在migration文件夹里面的initial.py文件里面

class Migration(migrations.Migration):  
  
initial = True  
  
dependencies = [  
]  
  
operations = [  
migrations.CreateModel(  
name='test',  
fields=[  
('id', models.AutoField(primary_key=True, serialize=False)),  
('username', models.CharField(max_length=64)),  
('password', models.CharField(max_length=64)),  
],  
),  
]

但是此时还是没有表,我们继续往下走

操作:在Pycharm的终端里面执行python3.10 manage.py migrate 这句话的作用才是真正把表创建起来。

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, demo0726, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying demo0726.0001_initial... OK
  Applying sessions.0001_initial... OK

当我们回到数据库里面会发现已经创建好了,但是会发现前面莫名其妙多了好多前缀。这个不用担心,只是Django为了防止表名冲突,默认添加的,后期可以取消掉。

ORM 增加 / 修改 / 删除 -- 字段

# 直接在类的下面写你的新字段
# 然后terminal里面执行迁移两句指令
# 直接在类的里面修改
# 执行migrate
# 直接在类里面删除
# 执行migrate

只要是跟类相关的改动,都需要执行

python3.10 manage.py makemigrations

python3.10 manage.py migrate

ORM 的查询

回到views.py文件。 from ... import models

语法:models.(表名).objects.filter(条件) objects就是一个小组件,里面封装了好多方法。

def login(request):  
if request.method == 'POST':  
  
# 获取输入的用户名和密码  
username = request.POST.get('username')  
password = request.POST.get('password')  
  
# 匹配数据库  
result = models.InfoBase.objects.filter(username=username,password=password).all()  
while len(result) > 0:  
name = result[0].username  
pswd = result[0].password  
  
# 判断用户名和密码是否正确
if username == name and password == pswd:  
print('Success!')  
break  
else:  
print('Wrong username/password. Please try again!')  
return render(request , 'login.html')

ORM 增加 / 修改 / 删除 -- 数据

# 查询 -- filter
result = models.InfoBase.objects.filter(username=username,password=password)

# 增加 -- create
result = models.InfoBase.objects.create(username=username,password=password)