萬里長征第二步——django我的博客(第四步 ——建立數據庫)

  1. 在models.py內設置數據庫模型
    # -*- coding=utf-8 -*-
    from __future__ import unicode_literals
    from django.db import models
    from django.contrib.auth.models import AbstractBaseUser
    # Create your models here.
    
    # 用戶模型.
    #第一種:採用的繼承方式擴展用戶信息(本系統採用)
    #擴展:關聯的方式去擴展用戶信息
    class User(AbstractBaseUser):
        avatar = models.ImageField(upload_to='avatar/%Y/%m', default='avatar/default.png', max_length=200, blank=True, null=True, verbose_name='用戶頭像')
        qq = models.CharField(max_length=20, blank=True, null=True, verbose_name='QQ號碼')
        mobile = models.CharField(max_length=11, blank=True, null=True, unique=True, verbose_name='手機號碼')
    
    class Meta:
            verbose_name = '用戶'
    verbose_name_plural = verbose_name
            ordering = ['-id']
    
    def __unicode__(self):
    return self.username

     

  2. 在setting.py內將數據庫類型 'DATABASES' 更改成mysql,並自定義要給model
    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'blogdb',
    'USER': 'root',
    'PASSWORD': '664390',
    'HOST': '',
    'POST': '',
        }
    }
    #自定義model
    AUTH_USER_MODEL = 'blog.User'

     

  3. 建立mysql數據庫,進入命令行
    C:\Users\66439>mysql -uroot -p664390  ——進入mysql數據庫
     
    mysql> CREATE DATABASE blogdb;  ——建立一個 'blogdb' 表
    Query OK, 1 row affected (0.00 sec)

     

  4. 進入虛擬環境
    D:\python\blog_project>d:\python\blog_project_venv\Scripts\activate

     

  5. 生成數據庫同步文件
    (blog_project_venv) D:\python\blog_project>manage.py makemigrations
    2016-05-25 23:16:29,582 [MainThread:9804] [django.db.backends:89] [utils:execute] [DEBUG]- (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
    Migrations for 'blog':
      0001_initial.py:
        - Create model Ad
        - Create model Article
        - Create model Catagory
        - Create model Category
        - Create model Comment
        - Create model Links
        - Create model Tag
        - Create model User
        - Add field user to comment
        - Add field category to article
        - Add field tag to article

    makemigrations blog ——帶上 'blog' ,只生成 'blog' 內的 'models.py'python

        - Add field user to article

     

  6. 同步數據庫
    (blog_project_venv) D:\python\blog_project>manage.py migrate
相關文章
相關標籤/搜索