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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章