Python 从0开始 一步步基于Django创建项目(2)创建应用程序&数据模型

本文内容建立在《Python 从0开始 一步步基于Django创建项目(1)》的基础上。

Django项目由一系列应用程序组成。

本文将创建一个'应用程序',其中包含两个'数据类'(数据模型),每个'数据类'有若干不同类型的数据成员。

将'应用程序'包含到Django项目中,以使用这些'数据模型'。

在数据库中,为'数据模型'生成对应表格,存储相关数据。

以下逐步完成。

1、创建应用程序

1)将路径切换到虚拟环境的Scripts文件夹,运行其中的activate,激活虚拟环境

python 复制代码
C:\D\Python\Python310\study\snap_gram>cd C:\D\Python\Python310\study\snap_gram\sg_env\Scripts

C:\D\Python\Python310\study\snap_gram\sg_env\Scripts>activate

(sg_env) C:\D\Python\Python310\study\snap_gram\sg_env\Scripts>

以上为Windows系统的激活方法。

2)切换回根目录

python 复制代码
(sg_env) C:\D\Python\Python310\study\snap_gram>

3)执行命令,创建项目city_infos,在manage.py文件所在的根目录,生成名为city_infos的文件夹。数据模型就定义在该文件夹的models.py文件中。

python 复制代码
(sg_env) C:\D\Python\Python310\study\snap_gram>python manage.py startapp city_infos

2、编写应用程序的数据类。

1)打开文件models.py,其路径是:C:\D\Python\Python310\study\snap_gram\city_infos

2)编写数据类,这里编写了两个数据类,城市类,城市信息类。

python 复制代码
from django.db import models

# Create your models here.

class City(models.Model):
    '''city name'''
    name = models.CharField(max_length=100)
    date_added = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural = 'cities'#正确显示city的复数形式

    def __str__(self):
        '''return city name'''
        return self.name


class Entry(models.Model):
    '''information of city'''

    #将info关联的city,一个city可以关联多个info,当删除city时级联删除info
    city = models.ForeignKey(City, on_delete=models.CASCADE)
    info = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        '''return city info'''
        return f"{self.info[:50]}..."

3、激活数据模型

1)打开文件settings.py,其路径是:C:\D\Python\Python310\study\snap_gram\snap_gram

2)修改文件内容如下

python 复制代码
# Application definition

INSTALLED_APPS = [
    #my app
    'city_infos',
    #auto app
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

4、为数据模型修改数据库

1)在控制台输入命令python manage.py makemigrations city_infos,生成数据库更新文件0001_initial,其路径是:C:\D\Python\Python310\study\snap_gram\city_infos\migrations

python 复制代码
(sg_env) C:\D\Python\Python310\study\snap_gram>python manage.py makemigrations city_infos
Migrations for 'city_infos':
  city_infos\migrations\0001_initial.py
    - Create model City
    - Create model Entry

(sg_env) C:\D\Python\Python310\study\snap_gram>

2)在控制台输入命令python manage.py migrate,使用更新文件0001_initial更新数据库

python 复制代码
(sg_env) C:\D\Python\Python310\study\snap_gram>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, city_infos, contenttypes, sessions
Running migrations:
  Applying city_infos.0001_initial... OK

(sg_env) C:\D\Python\Python310\study\snap_gram>

注意:以后每次对'数据模型'进行修改,都需要重复本部分操作,即生成数据库更新文件,使用更新文件更新数据库。

相关推荐
文人sec几秒前
接口自动化测试设计思路--设计实战
python·https·单元测试·自动化·pytest
Bl_a_ck12 分钟前
【React】Craco 简介
开发语言·前端·react.js·typescript·前端框架
子燕若水16 分钟前
Flask 调试的时候进入main函数两次
后端·python·flask
编程有点难17 分钟前
Python训练打卡Day23
开发语言·python
程序员爱钓鱼18 分钟前
跳转语句:break、continue、goto -《Go语言实战指南》
开发语言·后端·golang·go1.19
hardStudy_h25 分钟前
C程序的存储空间分配
c语言·开发语言
橙子1991101626 分钟前
Kotlin 中的 Unit 类型的作用以及 Java 中 Void 的区别
java·开发语言·kotlin
yours_Gabriel33 分钟前
【登录认证】JWT令牌
java·开发语言·redis
qq_14182697321 小时前
python通过curl访问deepseek的API调用案例
java·数据库·python
lyw2056191 小时前
微服务八股(自用)
java·开发语言