django网站制作(6-2)ORM模型操作数据库

目录

一、创建和映射ORM模型(Lesson47)

二、通过ORM操作数据库

四、创建ORM模型

五、迁移——django与数据库关联

六、数据库操作


 

一、创建和映射ORM模型(Lesson47)

(一)创建ORM模型

注意:django编码中,使用camelcase编码样式,即每个变量逻辑断点首字母大写。如:MyReprotZnWork。

ORM:object relational mapping,对象关系映射。可以通过类的方式操作数据库,从而不必写原生SQL语句。

在pycharm中打开已有项目文件,在编辑models.py中创建或编辑类模型。

1、一个模型类相当于数据库表;

2、通过把表写成类,列写作属性;

3、行写作实例。一个模型对象,对象数据表的一条数据。

from django.db import models

# Create your models here.
# 创建一个模型类,对应数据库中一张表


class ReportZnWork(models.Model):

    # 模型中的一个属性,对应表中的一个字段
    ReportZnWorkId = models.AutoField(primary_key=True)
    Teams = models.CharField(max_length=100, null=False)
    Output = models.FloatField(default=0)

    # 一个模型对象,对象数据表的一条数据
    # 将模型对应的APP添加到settings.py中的INSTALLED_APP中。
    # CMD命令窗口,该项目PYTHON虚拟环境下,进入项目所在目录,使用MakeMigrations生成迁移脚本文件,命令:python manage.py MakeMigrations
    # 使用migrate将新生成的迁移脚本文件映射到数据库中。命令:python manage.py migrate

最终将在数据库中生成名称为“znwork_reportznwork”表。(reportznwork为项目名称)

(二)ORM与数据库映射

模型创建后,将在pycharm信息窗口提示:

You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

其中:admin,auth,contenttypes,sessions,是创建项目时系统自动生成的APP。

1、须将模型对应的APP添加到项目settings.py中的INSTALLED_APPS中;

2、生成迁移脚本。命令:打开CMD命令窗口,WORKON [环境名]进入项目虚拟python环境,进入项目目录,执行:python manage.py MakeMigrations。(因为manage.py在项目目录下)

3、执行命令:python manage.py migrate。

$ python manage.py migrate   # 创建表结构

$ python manage.py makemigrations TestModel  # 让 Django 知道我们在我们的模型有一些变更
$ python manage.py migrate TestModel   # 创建表结构

Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They’re designed to be mostly automatic, but you’ll need to know when to make migrations, when to run them, and the common problems you might run into.

The Commands:migrate,makemigrations,sqlmigrate,showmigrations

migrate与migration区别:

Migrations | Django documentation | Django  https://docs.djangoproject.com/en/2.0/topics/migrations/

django-admin and manage.py | Django documentation | Django  https://docs.djangoproject.com/en/2.0/ref/django-admin/

二、通过ORM操作数据库

database schema:数据库模式、数据库架构

migrate, which is responsible for applying and unapplying migrations.

makemigrations, which is responsible for creating new migrations based on the changes you have made to your models.

You should think of migrations as a version control system for your database schema. makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

四、创建ORM模型

Models and databases | Django documentation | Django  https://docs.djangoproject.com/en/2.0/topics/db/

Models | Django documentation | Django  https://docs.djangoproject.com/en/2.0/topics/db/models/

What is WSGI? — WSGI.org  http://wsgi.readthedocs.io/en/latest/what.html

cis/TestModel/models.py: 文件代码:
# models.py
from django.db import models
 
class Test(models.Model):
    name = models.CharField(max_length=20)

以上的类名代表了数据库表名,且继承了models.Model,类里面的字段代表数据表中的字段(name),数据类型则由CharField(相当于varchar)、DateField(相当于datetime), max_length 参数限定长度。

在settings.py中找到INSTALLED_APPS这一项,将已建APP名称加入。

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'TestModel',               # 添加此项
)

五、迁移——django与数据库关联

$ python manage.py migrate   # 创建表结构

$ python manage.py makemigrations TestModel  # 让 Django 知道我们在我们的模型有一些变更
$ python manage.py migrate TestModel   # 创建表结构

Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They’re designed to be mostly automatic, but you’ll need to know when to make migrations, when to run them, and the common problems you might run into.

The Commands:migrate,makemigrations,sqlmigrate,showmigrations

migrate与migration区别:

Migrations | Django documentation | Django  https://docs.djangoproject.com/en/2.0/topics/migrations/

django-admin and manage.py | Django documentation | Django  https://docs.djangoproject.com/en/2.0/ref/django-admin/

database schema:数据库模式、数据库架构

migrate, which is responsible for applying and unapplying migrations.

makemigrations, which is responsible for creating new migrations based on the changes you have made to your models.

You should think of migrations as a version control system for your database schema. makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

六、数据库操作

接下来我们在 cis 目录中添加 testdb.py 文件(下面介绍),并修改 urls.py:

#_*_coding:UTF-8_*_
from django.http import HttpResponse
from TestModel.models import Test
def testdb(request):
  test1=Test(name='runoob')
  test1.save()
  return HttpResponse("<p>数据添加成功</p>")

并修改cis/cis/urls.py:文件代码:

from django.contrib import admin
from django.urls import path,re_path
from . import testdb
urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^testdb$',testdb.testdb),   #在新的版本不建议url函数,如:url(r'^testdb$', testdb.testdb),
]

django.urls functions for use in URLconfs | Django documentation | Django  https://docs.djangoproject.com/en/2.0/ref/urls/#django.urls.re_path

 

URL dispatcher | Django documentation | Django  https://docs.djangoproject.com/en/2.0/topics/http/urls/

lesson48

发布了306 篇原创文章 · 获赞 114 · 访问量 117万+

猜你喜欢

转载自blog.csdn.net/sjpljr/article/details/103056955
今日推荐