Django怎么编写数据模型类
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,本篇内容介绍了"Django怎么编写数据模型类"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!设计数据
千家信息网最后更新 2025年12月03日Django怎么编写数据模型类
本篇内容介绍了"Django怎么编写数据模型类"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
设计数据库和表结构是做网站的基础。在Django中,不需要通过SQL语句直接跟数据库打交道,而是完全用Python的类来创建数据模型,之后交给Django完成创建数据库的操作。
数据模型类
数据模型类需要在 应用目录 下的
models.py文件中编写
编写数据模型
下面的代码演示了在 models.py 中定义了一个博客文章的类
from django.db import modelsfrom django.utils import timezonefrom django.contrib.auth.models import User# Create your models here.class BlogArticles(models.Model): # Django中的数据模型类都继承自 django.db.models.Model类 # 字段 title 的属性为 CharField 类型,并且参数长度为 300 title = models.CharField(max_length=300) """ 字段 author 使用 ForeignKey 规定了博客文章和用户之间的关系:一个用户对应多篇文章 models.CASCADE 表示级联删除 related_name="blog_posts" 表示允许User类的实例以 "blog_posts" 属性反向查询到BlogArticles类的实例 """ author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="blog_posts") body = models.TextField() publish = models.DateTimeField(default=timezone.now) """ ordering = ("-publish",) 规定BlogArticles类的实例按 publish 字段值倒序显示 """ class Meta: ordering = ("-publish",) """ 重写父类的方法,使得当使用 str()函数转换该类的实例为字符串时,返回 title 属性的值 """ def __str__(self): return self.title根据数据模型建立数据库表
创建数据库表文件
E:\PycharmProjects\demosite>python manage.py makemigrationsMigrations for 'blog': blog\migrations\0002_auto_20190720_1919.py - Alter field publish on blogarticles
上面执行结果的提示信息中,告诉我们在
blog/migrations目录中创建了一个 BlogArticles模型,模型编号为 0001。可以输入以下命令查看相对应的SQL语句:
E:\PycharmProjects\demosite>python manage.py sqlmigrate blog 0001System check identified some issues:WARNINGS:blog.BlogArticles.publish: (fields.W161) Fixed default value provided. HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`BEGIN;---- Create model BlogArticles--CREATE TABLE "blog_blogarticles" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(300) NOT NULL, "body" text NOT NULL, "publish" datetime NOT NULL, "author_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);CREATE INDEX "blog_blogarticles_author_id_ed798e23" ON "blog_blogarticles" ("author_id");COMMIT;在数据库中表名格式为:
小写的应用名称_小写的类名称
创建数据库
(demosite) E:\PycharmProjects\demosite>python manage.py migrateSystem check identified some issues:WARNINGS:blog.BlogArticles.publish: (fields.W161) Fixed default value provided. HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`Operations to perform: Apply all migrations: admin, auth, blog, contenttypes, sessionsRunning 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 blog.0001_initial... OK Applying sessions.0001_initial... OK
"Django怎么编写数据模型类"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
数据
模型
数据库
实例
字段
属性
文章
内容
博客
小写
文件
更多
用户
目录
知识
网站
语句
应用
学有所成
接下来
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
西宁行业专业软件开发
计算机网络技术模测1
全球学术快报是数据库吗
软件开发 领导
福州今企助力软件开发
济南迪卡软件开发有限公司
纳塔服务器
神力科莎竞技服务器崩溃
软件开发者选项在哪里找
云服务器可以打开游戏吗
数据库增加字母
天泽服务器
俄乌网络安全战整体状况
周村微信小程序软件开发报价
必联路由器服务器名称填什么
网络安全关于那些
做么地方能恢复手机数据库
计算机网络技术河南大专
sql中删除某列数据库
哪家网站服务器好
饥荒服务器怎么给更大的内存
网络安全考研几个志愿
2020网络安全知识宣传
AD是硬件还是软件开发
德州服务器管理系统模式
一次性付费永久服务器
天泽服务器
软件开发和数据库开发的区别
网络安全法规知识宣贯
制度对网络安全