定义数据模型&访问数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了定义数据模型&访问数据库相关的知识,希望对你有一定的参考价值。
定义数据模型
一、Django定义数据模型在App中的models.py文件,数据库的表名称以类的形式来定义:
[[email protected] web]# cd /opt/python/django/web/blog/ [[email protected] blog]# vim models.py from django.db import models # Create your models here. class Host(models.Model): hostname = models.CharField(max_length = 50) ip = models.IPAddressField()
二、查看模型的语法和逻辑是否正确:python manage.py validate , 0 errors 没有语法错误
[[email protected] blog]# cd /opt/python/django/web [[email protected] web]# ls blog db.sqlite3 manage.py web [[email protected] web]# python manage.py validate 0 errors found
三、管理数据库
初始化数据模型到数据库:python manage.py syncdb (默认使用的slqite数据库,在setting.py可以看到)
[[email protected] web]# cd /opt/python/django/web/web/ [[email protected] web]# vim settings.py import os BASE_DIR = os.path.dirname(os.path.dirname(__file__))#base_dir是seting.py的上级目录的上级目录:/opt/python/django/web/ DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.sqlite3‘, ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘), } } 前提是安装sqlite [[email protected] web]# cd /opt/python/django/web/ [[email protected] web]# python manage.py dbshell SQLite version 3.6.20 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> .tables sqlite> .exit #后台演示同步数据库的时候数据库的创建过程, [[email protected] web]# python manage.py sqlall blog BEGIN; CREATE TABLE "blog_host" ( "id" integer NOT NULL PRIMARY KEY, "hostname" varchar(50) NOT NULL, "ip" char(15) NOT NULL ) ; COMMIT; #同步数据库,会创建表blog_host,创建管理用户 [[email protected] web]# python manage.py syncdb Creating tables ... Creating table django_admin_log Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_groups Creating table auth_user_user_permissions Creating table auth_user Creating table django_content_type Creating table django_session Creating table blog_host You just installed Django‘s auth system, which means you don‘t have any superusers defined. Would you like to create one now? (yes/no): yes Username (leave blank to use ‘root‘): root #创建管理用户 Email address: [email protected] #输入管理用户的邮箱 Password: #输入管理用户的密码 Password (again): Superuser created successfully. Installing custom SQL ... Installing indexes ... Installed 0 object(s) from 0 fixture(s) [[email protected] web]# ll 总用量 48 drwxr-xr-x 3 root root 4096 1月 3 09:50 blog -rw-r--r-- 1 root root 34816 1月 3 10:12 db.sqlite3 #大小不为0 -rwxr-xr-x 1 root root 246 1月 1 23:11 manage.py drwxr-xr-x 2 root root 4096 1月 3 10:02 web
四、访问数据库:
方法一:命令行方式访问
[[email protected] web]# python manage.py dbshell
方法二:通过admin的页面管理数据库
1、启功django [[email protected] web]# nohup python manage.py runserver 11.65.140.13:8080 & 2、在chrome浏览器中访问: 输入用户名root和密码,默认是看不到数据库
需要把表注册到admin.py中,admin才能识别
[[email protected] blog]# cd /opt/python/django/web/blog/ [[email protected] blog]# vim admin.py from django.contrib import admin from blog.models import Host #加载app应用models # Register your models here. class HostAdmin(admin.ModelAdmin): list_display = [‘hostname‘, ‘ip‘] #固定属性,类似表中的字段 admin.site.register(Host,HostAdmin) #注册两个表, 刷新网页,多了一个host列,点击增加一个host,输入主机名和IP,点击保存,多了一个主机,可以查看
使用命令行查看表
[[email protected] blog]# cd /opt/python/django/web/ [[email protected] web]# ll 总用量 52 drwxr-xr-x 3 root root 4096 1月 3 10:28 blog -rw-r--r-- 1 root root 34816 1月 3 10:32 db.sqlite3 -rwxr-xr-x 1 root root 246 1月 1 23:11 manage.py -rw------- 1 root root 2125 1月 3 10:37 nohup.out drwxr-xr-x 2 root root 4096 1月 3 10:02 web [[email protected] web]# sqlite3 db.sqlite3 SQLite version 3.6.20 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> .tables auth_group auth_user_user_permissions auth_group_permissions blog_host auth_permission django_admin_log auth_user django_content_type auth_user_groups django_session sqlite> select * from blog_host; 1|132|112.65.140.132 sqlite>.exit [[email protected] web]# python manage.py dbshell sqlite> .tables auth_group auth_user_user_permissions auth_group_permissions blog_host auth_permission django_admin_log auth_user django_content_type auth_user_groups django_session sqlite> select * from blog_host; 1|132|112.65.140.132
本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1888494
以上是关于定义数据模型&访问数据库的主要内容,如果未能解决你的问题,请参考以下文章