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>

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

相关推荐
郝学胜-神的一滴1 分钟前
Python数据封装与私有属性:保护你的数据安全
linux·服务器·开发语言·python·程序人生
悟能不能悟3 分钟前
Elastic Stack 中两种主要查询语言 KQL (Kibana Query Language) 和 Lucene 的详细对比和解释。
java·开发语言
智航GIS9 分钟前
11.7 使用Pandas 模块中describe()、groupby()进行简单分析
python·pandas
Pyeako13 分钟前
机器学习--矿物数据清洗(六种填充方法)
人工智能·python·随机森林·机器学习·pycharm·线性回归·数据清洗
赛恩斯40 分钟前
kotlin 为什么可以在没有kotlin 环境的安卓系统上运行的
android·开发语言·kotlin
steem_ding40 分钟前
net.core 调优指南
开发语言·php
ScilogyHunter1 小时前
SCons:Python驱动的智能构建系统
python·构建系统·scons
luoluoal1 小时前
基于python的基于深度学习的车俩特征分析系(源码+文档)
python·mysql·django·毕业设计·源码