django创建model

本文介绍了在Django中创建Model的步骤,包括新建应用、定义Model类、执行migrate命令以同步数据库。默认使用SQLite,但可以切换到MySQL,并详细说明了如何配置MySQL数据库及查看生成的表。此外,Django还会自动生成一个自动递增的ID字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

django创建model

1. steps:

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

2. model class

很简单,在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 来显示出来了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值