Django的Models

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/84671448

一 Django中的Models是什么

  • 通常,一个Model对应数据库的一张数据表。

  • Django中Models以类的形式表现。

  • 它包含了一些基本字段以及数据的一些行为。

二 ORM

  • 对象关系映射(Object Relation Mapping)

  • 描述了对象和数据库之间的映射

  • 隐藏了数据访问的细节,不需要编写SQL语句

三 编写Models

1 在应用根目录下创建models.py,并引入models模块

2 创建类,继承models.Model,该类即一张数据表

3 在类中创建字段

4 字段创建

5 创建Article模型

from __future__ import unicode_literals

from django.db import models

class Article(models.Model):
    title = models.CharField(max_length=32,default='Title')
    content = models.TextField(null=True)

四 生成数据表

1 步骤

1.1 命令行进入manage.py同级目录

1.2 执行python manage.py makemigrations app名(可选)

1.3 再执行python manage.py migrate

2 实践

E:\Django\myblog>python manage.py makemigrations
Migrations for 'blog':
  blog\migrations\0001_initial.py:
    - Create model Article

E:\Django\myblog>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, 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 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 blog.0001_initial... OK
  Applying sessions.0001_initial... OK

五 查看表的方法

1 Django会自动在app/migrations/目录下生成移植文件

# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2018-12-01 01:35
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
    initial = True
    dependencies = [
    ]
    operations = [
        migrations.CreateModel(
            name='Article',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(default='Title', max_length=32)),
                ('content', models.TextField(null=True)),
            ],
        ),
    ]

2 执行python manage.py sqlmigrate 应用名 文件id 查看SQL语句

PS E:\Django\myblog> python manage.py sqlmigrate blog 0001
BEGIN;
--
-- Create model Article
--
CREATE TABLE "blog_article" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(32) NOT NULL, "content" text NULL);
COMMIT;

3 默认sqlite3的数据库在项目根目录下db.sqlite3

4 编辑数据库中数据

六 页面展现——后端

1 步骤

1.1 views.py中import models

1.2 article = models.Article.object.get(pk=1)

1.3 render(request,page,{'article':article})

2 实践

2.1 编辑后端views.py

from django.shortcuts import render
from django.http import HttpResponse
from . import models

def index(request):
    article = models.Article.objects.get(pk=1)
    return render(request, 'blog/index.html',{'article': article})

七 页面展现——前端

模板可直接使用对象以及对象“.”操作{{article:title}}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>{{ article.title }}</h1>
<h3>{{ article.content }}</h3>
</body>
</html>

八 显示结果

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/84671448