日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python【算法中心 02】Web框架Django管理页面使用(管理员账号创建+API使用+应用添加)GreenPlum数据库引擎及API测试

發布時間:2024/10/6 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python【算法中心 02】Web框架Django管理页面使用(管理员账号创建+API使用+应用添加)GreenPlum数据库引擎及API测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.SQLite管理員賬號創建

SQLite 是 Django 默認的數據庫體量上類似與 Apache Derby,配置信息如下:

DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3','NAME': BASE_DIR / 'db.sqlite3',} }

makemigrations 為模型的改變生成遷移文件,migrate 實現應用數據庫遷移,切換數據庫或者添加新的module后要執行以上命令:

python .\manage.py migrate

否則創建管理員賬號時會報錯no such table: auth_user當然,未創建admin賬號就登錄同樣報這個錯:

OperationalError at /admin/login/ no such table: auth_user # 創建管理員賬號 admin python .\manage.py createsuperuser # 輸入管理員賬號名稱 Username (leave blank to use 'administrator'): admin_test # 輸入Email地址 Email address: admin_test@example.com # 輸入密碼 Password: Password (again): The password is too similar to the username. Bypass password validation and create user anyway? [y/N]: y # 創建成功 Superuser created successfully.

頁面登錄http://host:port/admin/輸入賬號名稱和密碼登錄:

2.GreenPlum管理頁面使用

2.1 數據庫配置及初始化

Django 支持許多不同的數據庫服務器,官方支持 PostgreSQL、MariaDB、MySQL、Oracle 和 SQLite。其他 DataBase Bindings 信息可以查看官網,這里僅以 PostgreSQL 的孿生兄弟 GreenPlum 數據庫舉例,也算是測試對 GP 數據庫的支持情況。ENGINE用的也是postgresql的,其他信息如下:

DATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql','NAME': 'algorithmcenter','USER': 'xxxx','PASSWORD': 'xxxx','HOST': 'hostname','PORT': '5432',} }

使用 PostgreSQL,需要 psycopg2 包,使用 GreenPlum 同樣是需要的,安裝 psycopg2-binary 命令如下:

# 安裝命令 pip3 install psycopg2-binary # 安裝過程 Collecting psycopg2-binaryDownloading psycopg2_binary-2.9.3-cp38-cp38-win_amd64.whl (1.1 MB)|████████████████████████████████| 1.1 MB 21 kB/s Installing collected packages: psycopg2-binary Successfully installed psycopg2-binary-2.9.3

migrate時報錯:

django.db.utils.IntegrityError: UNIQUE index must contain all columns in the distribution key of relation "django_content_type"

由于GP數據庫跟PG數據庫還是存在一些語法不一致的地方,這里將SQLite里的10張表同步到GP并創建auth_user表id字段的自增序列:

CREATE SEQUENCE greenplum_sequence START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; ALTER TABLE auth_user ALTER COLUMN ID SET DEFAULT nextval( 'greenplum_sequence' );

修改django_session表的expire_date字段格式由text 改為 timestamp否則登錄時報錯。

2.2 管理員賬號創建

# 賬戶創建 python .\manage.py createsuperuser Username (leave blank to use 'administrator'): test Email address: test@qq.com Password: Password (again): The password is too similar to the username. This password is too short. It must contain at least 8 characters. This password is too common. Bypass password validation and create user anyway? [y/N]: y Superuser created successfully.

登錄成功頁面不再貼出。

2.3 Django數據庫API使用

簡單測試,更多API查看 官網。

from xxxx.models import Choice, Question Question.objects.all() from django.utils import timezone q = Question(question_text="What's new?", pub_date=timezone.now()) q.save() q.id q.question_text q.pub_date q.question_text = "What's up?" q.save() Question.objects.all()

2.4 應用添加

在應用文件夾下的admin.py文件內添加注冊代碼:

# Register your models here. from .models import Question admin.site.register(Question)


新增數據:


修改數據:

3.總結

Django 對 GreenPlum 數據庫的支持不好,幾乎不可用,可以非官方支持的數據庫可放棄使用其數據庫 API。

總結

以上是生活随笔為你收集整理的Python【算法中心 02】Web框架Django管理页面使用(管理员账号创建+API使用+应用添加)GreenPlum数据库引擎及API测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。