django-模型层

时间:2021-08-06
本文章向大家介绍django-模型层,主要包括django-模型层使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

django模型层




settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysite2',
        'USER':'root',
        'PASSWORD':'password',
        'HOST':'localhost', #'HOST':'127.0.0.1'
        'PORT':'3306'
    }
}

orm框架




settings.py

# E:\django_video_study\mysite2>python manage.py startapp bookstore

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

bookstore.models.py

from django.db import models

# Create your models here.
class  Book(models.Model):
    titel=models.CharField('书名',max_length=50,default='')
    price=models.DecimalField('价格',max_digits=7,decimal_places=2)

执行生成迁移文件

E:\django_video_study\mysite2>python manage.py makemigrations
Migrations for 'bookstore':
  bookstore\migrations\0001_initial.py
    - Create model Book

E:\django_video_study\mysite2\bookstore>tree
卷 NewDisk 的文件夹 PATH 列表
卷序列号为 F077-0FBF
E:.
├─migrations
│  └─__pycache__
└─__pycache__

迁移文件 bookstore/migrations/0001_initial.py

# Generated by Django 2.2.23 on 2021-08-06 01:35

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Book',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('titel', models.CharField(default='', max_length=50, verbose_name='书名')),
                ('price', models.DecimalField(decimal_places=2, max_digits=7, verbose_name='价格')),
            ],
        ),
    ]


创建迁移

E:\django_video_study\mysite2>python 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 bookstore.0001_initial... OK
  Applying sessions.0001_initial... OK

迁移成功显示mysql数据

mysql> use mysite2
Database changed
mysql> show tables;
+----------------------------+
| Tables_in_mysite2          |
+----------------------------+
| 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    | int          | NO   | PRI | NULL    | auto_increment |
| titel | varchar(50)  | NO   |     | NULL    |                |
| price | decimal(7,2) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)


原文地址:https://www.cnblogs.com/yescarf/p/15107171.html