zoukankan      html  css  js  c++  java
  • Django 初始化数据库

    django 初始化数据库

    刷新数据库
    guoguos-MacBook-Pro:mysite guoguo$ python manage.py sqlflush
    BEGIN;
    SET FOREIGN_KEY_CHECKS = 0;
    TRUNCATE `auth_user`;
    TRUNCATE `auth_group`;
    TRUNCATE `auth_user_user_permissions`;
    TRUNCATE `django_content_type`;
    TRUNCATE `auth_group_permissions`;
    TRUNCATE `django_session`;
    TRUNCATE `django_admin_log`;
    TRUNCATE `auth_user_groups`;
    TRUNCATE `auth_permission`;
    SET FOREIGN_KEY_CHECKS = 1;
    COMMIT;

    清空数据库的数据
    guoguos-MacBook-Pro:mysite guoguo$ python manage.py flush
    You have requested a flush of the database.
    This will IRREVERSIBLY DESTROY all data currently in the 'django' database,
    and return each table to an empty state.
    Are you sure you want to do this?

        Type 'yes' to continue, or 'no' to cancel: yes

    删除数据库表结构
    mysql> drop tables auth_permission;
    Query OK, 0 rows affected (0.01 sec)

    初始化数据库

    guoguos-MacBook-Pro:mysite guoguo$ python manage.py migrate
    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, sessions
    Running migrations:
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying admin.0002_logentry_remove_auto_add... OK
      Applying contenttypes.0002_remove_content_type_name... OK
      Applying auth.0002_alter_permission_name_max_length... OK
      Applying auth.0003_alter_user_email_max_length... OK
      Applying auth.0004_alter_user_username_opts... OK
      Applying auth.0005_alter_user_last_login_null... OK
      Applying auth.0006_require_contenttypes_0002... OK
      Applying auth.0007_alter_validators_add_error_messages... OK
      Applying auth.0008_alter_user_username_max_length... OK
      Applying sessions.0001_initial... OK
    guoguos-MacBook-Pro:mysite guoguo$
    guoguos-MacBook-Pro:mysite guoguo$
    guoguos-MacBook-Pro:mysite guoguo$
    guoguos-MacBook-Pro:mysite guoguo$

    在setting.py INSTALLED_APPS = 中增加 guoguo 项目

    INSTALLED_APPS = [
        'guoguo',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]


    在项目里生成一个表文件
    guoguos-MacBook-Pro:mysite guoguo$ python manage.py makemigrations guoguo
    Migrations for 'guoguo':
      guoguo/migrations/0001_initial.py
        - Create model Person

    生成表结构
    guoguos-MacBook-Pro:mysite guoguo$ python manage.py sqlmigrate guoguo 0001
    BEGIN;
    --
    -- Create model Person
    --
    CREATE TABLE `guoguo_person` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `first_name` varchar(30) NOT NULL, `last_name` varchar(30) NOT NULL);
    COMMIT;


    把表结构导入数据库


    guoguos-MacBook-Pro:mysite guoguo$ python manage.py migrate
    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, guoguo, sessions
    Running migrations:
      Applying guoguo.0001_initial... OK
     

  • 相关阅读:
    [Architecture Design] 跨平台架构设计
    [Architecture Pattern] Factory Builder
    Windows Phone 8初学者开发—第12部分:改进视图模型和示例数据
    Windows Phone 8初学者开发—第11部分:设置SounBoard应用程序
    Windows Phone 8初学者开发—第10部分:数据绑定应用程序和透视应用程序项目模板简介
    Windows Phone 8初学者开发—第9部分:Windows Phone 8模拟器概述
    Windows Phone 8初学者开发—第8部分:理解编译和部署
    Windows Phone 8初学者开发—第7部分:本地化应用程序
    Windows Phone 8初学者开发—第6部分:设置应用程序的样式
    Windows Phone 8初学者开发—第5部分:布局和事件基础
  • 原文地址:https://www.cnblogs.com/sxwen/p/7840308.html
Copyright © 2011-2022 走看看