您的位置:首页 > 数据库 > SQL

django MVC模式 数据库的操作mysql

2017-07-21 17:59 555 查看
介绍:本节课我们继续学习djangoWEB框架的开发,这节课主要是学习如何访问数据库,django如何自动为我们创建好表结构等相关内容。

1、首先我们打开settings.py找到DATABASES关键字,这个是配置我们的数据库。



里面的属性不做介绍了,一看就懂了。

2、添加自己的一个startapp polls,并在settings中配置。



也可以直接写'polls'

3、打开我们刚刚创建的应用polls -->models.py内容如下:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Test(models.Model):

name = models.CharField(max_length=20)

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

4、在pycharm中运行manage.py



成功后出现:



我们执行三条命令如下:

# 创建表结构(内置的django表结构)

manage.py@pythondjango > migrate
D:\PyCharm\bin\runnerw.exe D:\Python\python.exe D:\PyCharm\helpers\pycharm\django_manage.py migrate D:/pythondjango
Operations to perform:
Apply all migrations: admin, auth, 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 sessions.0001_initial... OK

Process finished with exit code 0

让 Django 知道我们在我们的模型有一些变更,生成一个文件

manage.py@pythondjango > makemigrations polls
D:\PyCharm\bin\runnerw.exe D:\Python\python.exe D:\PyCharm\helpers\pycharm\django_manage.py makemigrations polls D:/pythondjango
Migrations for 'polls':
polls\migrations\0001_initial.py
- Create model Test
Following files were affected
D:\pythondjango\polls\migrations\0001_initial.py
Process finished with exit code 0



文件内容是:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2017-07-21 09:15
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Test',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=20)),
],
),
]

其实里面记录了数据库的一些变化

manage.py@pythondjango > migrate polls
D:\PyCharm\bin\runnerw.exe D:\Python\python.exe D:\PyCharm\helpers\pycharm\django_manage.py migrate polls D:/pythondjango
Operations to perform:
Apply all migrations: polls
Running migrations:
Applying polls.0001_initial... OK

Process finished with exit code 0

表名组成结构为:应用名_类名



注意:尽管我们没有在models给表设置主键,但是Django会自动添加一个id作为主键

操作数据库:

在urls.py中添加一个映射:



向数据库中添加一个值,修改views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse

from polls.models import Test
from django.shortcuts import render

# Create your views here.

# 数据库操作
def testdb(request):
test1 = Test(name='温鸿雨')
test1.save()
return HttpResponse("<p>数据添加成功!</p>")

[b]添加成功:[/b]




总结:本节课主要分享django与mysql数据库的搭配使用,如何创建数据库表结构,settings文件中配置数据库,添加自己新创建的应用,如何在models.py中创建一个实例,属性的含义等内 容。同时借助pycharm自动生成数据库表,并且创建表结构是不需要指定主键,django会帮助我们创建一个id。在views.py中向数据库添加值的步骤。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: