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>

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

相关推荐
Kalika0-036 分钟前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
_.Switch38 分钟前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
代码雕刻家1 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
一个闪现必杀技1 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
Fan_web1 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
龙图:会赢的1 小时前
[C语言]--编译和链接
c语言·开发语言
小鹿( ﹡ˆoˆ﹡ )1 小时前
探索IP协议的神秘面纱:Python中的网络通信
python·tcp/ip·php
卷心菜小温2 小时前
【BUG】P-tuningv2微调ChatGLM2-6B时所踩的坑
python·深度学习·语言模型·nlp·bug
陈苏同学2 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
唐家小妹2 小时前
介绍一款开源的 Modern GUI PySide6 / PyQt6的使用
python·pyqt