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

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 23:13:31

django创建model的相关文章

Django 创建model的一些注意事项

自增主键字段¶ 默认情况下,Django 会给每个模型添加下面这个字段: id = models.AutoField(primary_key=True) 这是一个自增主键字段. 如果你想指定一个自定义主键字段,只要在某个字段上指定primary_key=True 即可.如果 Django 看到你显式地设置了 Field.primary_key,就不会自动添加 id 列. 每个模型只能有一个字段指定primary_key=True(无论是显式声明还是自动添加). 字段的自述名¶ 除ForeignK

django使用model创建数据库表使用的字段

Django通过model层不可以创建数据库,但可以创建数据库表,以下是创建表的字段以及表字段的参数.一.字段1.models.AutoField 自增列= int(11) 如果没有的话,默认会生成一个名称为 id 的列,如果要显示的自定义一个自增列,必须将给列设置为主键 primary_key=True.2.models.CharField 字符串字段 必须 max_length 参数3.models.BooleanField 布尔类型=tinyint(1) 不能为空,Blank=True4.

Django 中创建Model时报以下错误: TypeError: init() missing 1 required positional argument: ‘on_delete’

Django 中创建Model时报以下错误: TypeError: init() missing 1 required positional argument: 'on_delete' 代码如下: from django.db import models Create your models here. class Contract(models.Model): nid = models.AutoField(primary_key=True) name = models.CharField(ma

Django操作model时,报错:AttributeError:’ProgrammingError’ object has no attribute ‘__traceback__’

原因:在Django项目下相应的应用下面的models.py配置的model(也就是class)没有创建成相应的表. 这是怎么回事呢? 首先,将models.py里面的model创建成对应的数据库表的执行命令(DOS命令)为:manage.py syncdb. 但是我自己的电脑上执行该命令时,显示.Unknown command:syncdb.执行,manage.py help后的确没有发现这个子命令.最后网上搜索发现这个命令已经在Django1.9里面取消了.并且stackoverflow里面

Django(4) model

参考:https://docs.djangoproject.com/en/1.9/intro/tutorial02/ 1. 创建model 开打model文件,添加class(相当于table).class下包含不同variables(叫filed,相当于列/属性/column) 2.plug APP django的app是pluggable的. 1)在setting.py中的installed_apps中插入 'appname.apps.AppnameConfig' 2)产生migrate文件

django的Model 模型中常用的字段类型

常用的字段类型: AutoField:自增长字段,通常不用,如果未在Model中显示指定主键,django会默认建立一个整型的自增长主键字段 BooleanField:布尔型,值为True或False,在管理工具表现为checkbox CharField:单行字符串字段,CharField有一个必填参数:      CharField.max_length:字符的最大长度,django会根据这个参数在数据库层和校验层限制该字段所允许的最大字符数. TextField:textarea字段,即多行

Python基础(Django三——Model)

本篇内容接上篇Python基础(Django二) 七.Model 1.说明: Model是Django为方便程序操作数据库而诞生的,使用的是ORM模式. 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.简单的说,ORM是通过使用描述对象和数据库之间映射的关系,将程序中的对象自动持久化到关系数据库中. 2.使用: 2-1.创建Model(编辑应用目录下的models.py) from django.db

django创建一个简单的web站点

一.新建project 使用Pycharm,File->New Project-,选择Django,给project命名 (project不能用test命名) 新建的project目录如下: settings.py:工程相关的配置 urls.py:网站访问入口,对应到views wigs.py:部署相关 manage.py:执行命令 templates:html等静态文件 备注:新建工程后需要安装django的lib包,然后验证django是否安装成功 >>> import dja

Python之路【第二十二篇】:Django之Model操作

Django之Model操作 一.字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 primary_key=True 注:当model中如果没有自增列,则自动会创建一个列名为id的列 from django.db import models class UserInfo(models.Model): # 自动创建一个列名为id的且为自增的整数列 usern