Defining the Data Model & accessing the database

Source: Internet
Author: User
Tags auth parent directory sqlite

Defining the data Model



One, the Django definition data model in the app's models.py file, the database table name is defined in the form of a class:

[Email protected] web]# cd/opt/python/django/web/blog/[[email protected] blog]# vim models.pyfrom django.db Import Mode ls# Create Your models Here.class Host (models. Model): hostname = models. Charfield (max_length =) IP = models. Ipaddressfield ()


Second, check the model syntax and logic is correct: Python manage.py validate, 0 errors no syntax error

[Email protected] blog]# cd/opt/python/django/web[[email protected] web]# lsblog db.sqlite3 manage.py web[[email Prot ected] web]# python manage.py validate0 errors found


III. Management Database

Initialize the data model to the database: Python manage.py syncdb (the Slqite database used by default, as setting.py can see)

[[email protected] web]# cd /opt/python/django/web/web/[[email protected] web]#  vim settings.pyimport osbase_dir = os.path.dirname (Os.path.dirname (__file__)) #base_ Dir is the parent directory of the parent directory of seting.py:/opt/python/django/web/databases = {     ' default ':  {         ' ENGINE ':  ' django.db.backends.sqlite3 ',          ' NAME ':  os.path.join (base_dir,  ' db.sqlite3 '),     }} If the installation is sqlite[[email protected] web]# cd  /opt/python/django/web/[[email  protected] web]# python manage.py dbshellsqlite version 3.6.20enter  ". Help"  for instructionsEnter SQL statements terminated with a  ";" sqlite> .tablessqlite> .exit# Background Demo Database creation process when synchronizing the database,[[email protected] web]#  python manage.py sqlall&Nbsp;blogbegin; create table  "Blog_host"   (     "id"  integer NOT NULL  primary key,     "hostname"  varchar ( NOT NULL,   )   "IP"  char ( not null); COMMIT; #同步数据库, create a table blog_host, create an administrative user [[email protected] web]# python manage.py syncdb   CREATING TABLES&NBSP, ..... Creating table django_admin_logcreating table auth_permissioncreating table auth _group_permissionscreating table auth_groupcreating table auth_user_groupscreating  Table auth_user_user_permissionscreating table auth_usercreating table django_content _typecreating table django_sessioncreating table blog_hostyou just installed  django ' S auth system, which means you don ' t have any  superusers defined. Would you like to create one now?  (yes/no): yesusername  (leave blank to  use  ' root '): root   #创建管理用户Email  address: [email protected]        # Enter the administrative user's mailbox password:                                      #输入管理用户的密码Password   (again):  superuser created  SUCCESSFULLY.  INSTALLING CUSTOM SQL&NBSP, ..... INSTALLING INDEXES&NBSP, ..... Installed 0 object (s)  from 0 fixture (s) [[Email protected] web]# ll Total dosage  48drwxr-xr-x 3 root root  4096 1 Month    3 09:50  Blog-rw-r--r-- 1 root root 34816 1 Month    3 10:12 db.sqlite3    #大小不为0-RWXR-XR-X 1&NBSp;root root   246 1 Month    1 23:11 manage.pydrwxr-xr-x 2  root root  4096 1 Month    3 10:02 web

Iv. Accessing the database:

Method One: command-line access

[email protected] web]# python manage.py Dbshell

Method Two: Manage the database via admin page

1, Qi Gong django[[email protected] web]# nohup python manage.py runserver 11.65.140.13:8080 &2, access in Chrome browser: Enter the user name root and password, the default is not to see the database

You need to register the table with the admin.py admin to recognize

[Email protected] blog]# cd/opt/python/django/web/blog/[[email protected] blog]# vim Admin.pyfrom django.contrib Import adminfrom blog.models Import Host # Loads the app models# Register your models here.class hostadmin (admin. Modeladmin): List_display = [' hostname ', ' IP '] #固定属性, fields similar to table Admin.site.register (host,hostadmin) # Register two tables, refresh the page, one more hos T column, click Add a host, enter the hostname and IP, click Save, more than one host, you can view

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/8C/64/wKiom1hrDzLQHWE2AACyfyLU6R8900.png-wh_500x0-wm_3 -wmp_4-s_2875665047.png "style=" Float:none; "title=" E37839da-e0ee-4b3b-93bf-89f72fbf01a0.png "alt=" Wkiom1hrdzlqhwe2aacyfylu6r8900.png-wh_50 "/>

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/8C/60/wKioL1hrDzPhSFooAADvbaW5mwA944.png-wh_500x0-wm_3 -wmp_4-s_1655198418.png "style=" Float:none; "title=" 3aceaf8a-3ce4-4b18-98e8-9981d2f18ef5.png "alt=" Wkiol1hrdzphsfooaadvbaw5mwa944.png-wh_50 "/>

To view a table using the command line

[[email protected] blog]# cd /opt/python/django/web/[[email protected] web]#  ll Total dosage  52drwxr-xr-x 3 root root  4096 1 month    3 10:28  blog-rw-r--r-- 1 root root 34816 1 Month    3 10:32  Db.sqlite3-rwxr-xr-x 1 root root   246 1 Month    1 23:11  MANAGE.PY-RW------- 1 root root  2125 1 Month    3 10:37  nohup.outdrwxr-xr-x 2 root root  4096 1 Month    3 10:02  web[[email protected] web]# sqlite3 db.sqlite3 SQLite version  3.6.20enter  ". Help"  for instructionsenter sql statements terminated with  a  ";" sqlite> .tablesauth_group                   auth_user_user_permissionsauth_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.132sqlite>.exit[[email protected] web]#  python manage.py dbshellsqlite> .tablesauth_group                   auth_user_user_permissionsauth_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


This article comes from "Plum blossom fragrance from bitter cold!" "Blog, be sure to keep this provenance http://daixuan.blog.51cto.com/5426657/1888494

Defining the Data Model & accessing the database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.