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>

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

相关推荐
hello_ejb34 分钟前
聊聊Spring AI Alibaba的PdfTablesParser
windows·python·spring
Kairo_016 分钟前
秘密任务 2.0:如何利用 WebSockets + DTOs 设计实时操作
python
ghost14311 分钟前
Python自学第2天:条件语句,循环语句
开发语言·python·学习
RunsenLIu13 分钟前
基于Django实现的图书分析大屏系统项目
后端·python·django
Chandler2423 分钟前
Go:低级编程
开发语言·后端·golang
小小福仔24 分钟前
Python爬虫第四战(使用防盗链下载视频)
爬虫·python·音视频·反爬·防盗链referer
明月看潮生36 分钟前
青少年编程与数学 02-016 Python数据结构与算法 23课题、分布式算法
分布式·python·算法·青少年编程·编程与数学
^_^ 纵歌37 分钟前
用python比较两个mp4是否实质相同
开发语言·python·音频·视频
一直走下去-明37 分钟前
使用python帮助艺术家完成角色动画和服装模型等任务
开发语言·图像处理·pytorch·python·opencv·ai作画
铭阳(●´∇`●)39 分钟前
Python内置函数---anext()
笔记·python·学习