zoukankan      html  css  js  c++  java
  • django创建model

    django创建model

    1. steps:

    1. new app
    2. add model class in app/models.py
    3. python manage.py migrate

    2. model class

    非常easy,在models.py里面定义一个继承models.Model的类就可以。

    # Create your models here.
    class Person(models.Model):
        name = models.CharField(max_length=30)

    然后python manage.py migrate

    root@git:~/project/test03# python manage.py migrate
    Operations to perform:
      Synchronize unmigrated apps: staticfiles, messages
      Apply all migrations: admin, blog, contenttypes, auth, sessions
    Synchronizing apps without migrations:
      Creating tables...
        Running deferred SQL...
      Installing custom SQL...
    Running migrations:
      Rendering model states... DONE
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... 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 blog.0001_initial... OK
      Applying sessions.0001_initial... OK

    3. see the database

    默认db配置在settings.py里面

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

    能够看到用了sqlite。


    sqlite3

    root@git:~/project/test03# sqlite3 db.sqlite3
    SQLite version 3.7.13 2012-06-11 02:05:22
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    sqlite> .schema blog_Person
    CREATE TABLE "blog_person" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(30) NOT NULL);
    sqlite>

    manage.py syncdb了以后,自己主动创建了一些表和自己定义的model Person的数据表。

    4. use mysql instead of sqlite

    首先要安装mysql的python库

    pip install mysql

    创建mysql数据库

    mysql> create database blog;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> grant all on blog.* to 'aca'@localhost identified by '123123';
    Query OK, 0 rows affected (0.00 sec)

    settings.py里面这么改

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'blog',
            'USER': 'aca',
            'PASSWORD': '123123',
            'HOST': 'localhost',
        }
    }
    root@git:~/project/test03# python manage.py migrate
    Operations to perform:
      Synchronize unmigrated apps: staticfiles, messages
      Apply all migrations: admin, blog, contenttypes, auth, sessions
    Synchronizing apps without migrations:
      Creating tables...
        Running deferred SQL...
      Installing custom SQL...
    Running migrations:
      Rendering model states... DONE
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... 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 blog.0001_initial... OK
      Applying sessions.0001_initial... OK

    进到mysql 里面看,

    mysql> desc blog_person
        -> ;
    +-------+-------------+------+-----+---------+----------------+
    | Field | Type        | Null | Key | Default | Extra          |
    +-------+-------------+------+-----+---------+----------------+
    | id    | int(11)     | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(30) | NO   |     | NULL    |                |
    +-------+-------------+------+-----+---------+----------------+
    2 rows in set (0.00 sec)

    另一点就是django会生成一个auto increment的id字段。

    之后就是用view 来显示出来了。

  • 相关阅读:
    满20年程序员生涯-与大家分享最近7年的快速成长经历(上海市青浦区快递行业战斗7年奋斗史)
    格局 逐阶而上
    基础才是重中之重~BouncyCastle实现的DES3加密~java通用
    jenkins~Publish Over SSH实现分布式部署
    maven~为MANIFEST.MF文件添加内容
    maven~多个plugin相同phase的执行顺序
    java~jar防止反编译
    个人博客的简单通告
    SQL Server中datetimeset转换datetime类型问题浅析
    MySQL如何计算统计redo log大小
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7210911.html
Copyright © 2011-2022 走看看