Django框架支持
Django框架支持¶
本文档描述了 SeaboxSQL 数据库对于DBunit框架的支持情况和配置说明,面向所有使用SeaboxSQL数据库的用户,主要是数据库管理员和应用程序开发人员。
有关 Django 的更多信息,请参阅以下资源: Django 文档https://dbunit.sourceforge.net/dbunit/index.html)
框架说明¶
Django 是一个高水准的 Python 编程语言驱动的开源模型,是视图、控制器风格的 Web 应用程序框架,它起源于开源社区。使用这种架构,程序员可以方便快捷地创建高品质、易维护、数据库驱动的应用程序。 相关的官方文档可以访问 https://docs.djangoproject.com 。
支持情况¶
由于 Django 的 SeaboxSQL方言包依赖 psycopg2,方言包的支持情况与 psycopg2 一致,如下表所示:
Python版本 | 系统架构 | 说明 |
---|---|---|
Python2.7 | Linux amd64 | 仅提供 64 位支持 |
Python2.7 | Linux aarch64 | 仅提供 64 位支持 |
Python2.7 | Linux mips64le | 仅提供 64 位支持 |
Python2.7 | Windows | 提供32和64位支持,32位需要VC9运行时,64位需要VC12 运行 |
Python3.5 | Linux amd64 | 仅提供 64 位支持 |
Python3.5 | Linux aarch64 | 仅提供 64 位支持 |
Python3.5 | Linux mips64le | 不支持 |
Python3.5 | Windows | 提供32和64位支持,需要VC12 运行 |
工具包说明¶
安装django与psycopg2工具包
pip install django
pip install psycopg2
Django使用说明¶
数据库连接¶
打开 settings.py 文件,然后修改 DATABASE 下的 default 键值进行修改即可(使用pg连接)。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '库名',
'USER': '用户名',
'PASSWORD': '密码',
'HOST': 'IP地址',
'PORT': '端口',
}
}
Django 不会自动创建数据库,因此需要手动创建数据库,即在数据库命令行交互模式执行以下命令:
create database 数据库名称
测试用例¶
创建Django project工程myproject和myTest应用
创建测试库和数据表
create database testdb;
drop table if exists user_info cascade;
create table user_info (
num_id serial --自增
, user_id integer not null
, user_name varchar(20) not null
, user_password varchar not null
, create_date timestamp not null
, create_user_id integer not null
, constraint user_info_PKC primary key (num_id, user_id) --表的主键
) ;
comment on table user_info is '用户信息';
comment on column user_info.num_id is '自增ID';
comment on column user_info.user_id is '用户ID';
comment on column user_info.user_name is '用户名';
comment on column user_info.user_password is '用户密码';
comment on column user_info.create_date is '创建日期';
comment on column user_info.create_user_id is '创建者';
insert into user_info
(user_id, user_name , user_password, create_date, create_user_id)
values
(1, 'zhangsan', '123456', now(), 0),
(2, 'lisi', '123456', now(), 0),
(3, 'wangwu', '123456', now(), 0),
(4, 'zhaoliu', '123456', now(), 0)
修改settings.py文件
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'testdb',
'USER': 'SeaboxSQL',
'PASSWORD': '123456',
'HOST': '192.168.2.168',
'PORT': '7300',
}
}
生成模型文件并添加到models.py中
python3 manage.py inspectdb > myTest/models.py
class UserInfo(models.Model):
num_id = models.AutoField(primary_key=True, db_comment='自增ID') # The composite primary key (num_id, user_id) found, that is not supported. The first column is selected.
user_id = models.IntegerField(db_comment='用户ID')
user_name = models.CharField(max_length=20, db_comment='用户名')
user_password = models.CharField(db_comment='用户密码')
create_date = models.DateTimeField(db_comment='创建日期')
create_user_id = models.IntegerField(db_comment='创建者')
class Meta:
managed = False
db_table = 'user_info'
unique_together = (('num_id', 'user_id'),)
db_table_comment = '用户信息'
创建testdb.py数据库查询测试文件
from myTest.models import UserInfo #引入模型
from django.http import HttpResponse
def userInfo(request):
user = UserInfo.objects.values() #查询数据
print("user=========", user)
return HttpResponse(user)
修改urls.py文件
from django.contrib import admin
from django.urls import path
from myTest import testdb
urlpatterns = [
path('admin/', admin.site.urls),
path('userInfo/',testdb.userInfo),
]
运行Django框架,浏览器访问userInfo连接查询数据并反馈到前台面页。
http://127.0.0.1:8000/userInfo/