django

Django-11 模型层及ORM介绍

2021-07-13  本文已影响0人  JuliusL

Django配置mysql

sudo pip3 install mysqlclient

创建数据库

创建好后如果报错:
NameError: name '_mysql' is not defined
需在项目的_init_.py文件下加

import pymysql 
pymysql.install_as_MySQLdb()

重新执行后如果报错
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.4.0 or newer is required; you have 0.10.1.
有一个好办法,直接指定版本,比其他的解决方法简单一些…

import pymysql
pymysql.version_info = (1, 4, 13, "final", 0)
pymysql.install_as_MySQLdb()

什么是模型

ORM框架

模型类代码示例

from django.db import models
# Create your models here.
class Book(models.Model):
    title = models.CharField("书名",max_length=50,default='')
    price = models.DecimalField('价格',max_digits=7,decimal_places=2,default=0.0)
    

数据库迁移

实操:

^CliujiadeMacBook-Pro:mysite1 liujia$ python3 manage.py startapp bookstore
liujiadeMacBook-Pro:mysite1 liujia$ python3 manage.py makemigrations
Migrations for 'bookstore':
  bookstore/migrations/0001_initial.py
    - Create model Book
liujiadeMacBook-Pro:mysite1 liujia$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, bookstore, 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 admin.0003_logentry_add_action_flag_choices... 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 auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying bookstore.0001_initial... OK
  Applying sessions.0001_initial... OK
liujiadeMacBook-Pro:mysite1 liujia$ 
mysql> use mysite1
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> show tables;
+----------------------------+
| Tables_in_mysite1          |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| bookstore_book             |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
11 rows in set (0.00 sec)

mysql> desc bookstore_book;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | bigint       | NO   | PRI | NULL    | auto_increment |
| title | varchar(50)  | NO   |     | NULL    |                |
| price | decimal(7,2) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.02 sec)

mysql> 

上一篇下一篇

猜你喜欢

热点阅读