Python contenttypes组件
介绍



应用场景
我们在网上po一段散文诗也可以po一张旅途的风景图,文字可以被评论,图片也可以被评论。我们需要在数据库中建表存储这些数据,我们可能会设计出下面这样的表结构。
class Post(models.Model):
"""帖子表"""
author = models.ForeignKey(User)
title = models.CharField(max_length=72) class Picture(models.Model):
"""图片表"""
author = models.ForeignKey(User)
image = models.ImageField() class Comment(models.Model):
"""评论表"""
author = models.ForeignKey(User)
content = models.TextField()
post = models.ForeignKey(Post, null=True, blank=True, on_delete=models.CASCADE)
picture = models.ForeignKey(Picture, null=True, blank=True, on_delete=models.CASCADE)
这表结构看起来不太简洁,我们画个图来看一下:

能用是能用,但是评论表有点冗余啊。好多列都空着呢啊!
我们优化一下,我们在评论表里不直接外键关联 文字和图片,而是存储一下关联的表名和字段,这样就好很多了。
看下图:

那我们不妨步子再大一点,再往前走一步试试,因为表名在评论里面重复了很多次,我们完全可以把Django项目中的表名都存储在一个表里面。然后评论表里外键关联这个表就可以了。

class Comment(models.Model):
"""评论表"""
author = models.ForeignKey(User)
content = models.TextField() content_type = models.ForeignKey(ContentType) # 外键关联django_content_type表
object_id = models.PositiveIntegerField() # 关联数据的主键
content_object = GenericForeignKey('content_type', 'object_id')
contenttypes使用
import os if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "about_contenttype.settings") import django
django.setup() from app01.models import Post, Picture, Comment
from django.contrib.auth.models import User
# 准备测试数据
user_1 = User.objects.create_user(username='aaa', password='123')
user_2 = User.objects.create_user(username='bbb', password='123')
user_3 = User.objects.create_user(username='ccc', password='123') post_1 = Post.objects.create(author=user_1, title='Python入门教程')
post_2 = Post.objects.create(author=user_2, title='Python进阶教程')
post_3 = Post.objects.create(author=user_1, title='Python入土教程') picture_1 = Picture.objects.create(author=user_1, image='小姐姐01.jpg')
picture_2 = Picture.objects.create(author=user_1, image='小姐姐02.jpg')
picture_3 = Picture.objects.create(author=user_3, image='小哥哥01.jpg') # 给帖子创建评论数据
comment_1 = Comment.objects.create(author=user_1, content='好文!', content_object=post_1)
# 给图片创建评论数据
comment_2 = Comment.objects.create(author=user_2, content='好美!', content_object=picture_1)
from django.contrib.contenttypes.fields import GenericRelation
class Post(models.Model):
"""帖子表"""
author = models.ForeignKey(User)
title = models.CharField(max_length=72) comments = GenericRelation('Comment') # 支持反向查找评论数据(不会在数据库中创建字段) class Picture(models.Model):
"""图片表"""
author = models.ForeignKey(User)
image = models.ImageField() comments = GenericRelation('Comment') # 支持反向查找评论数据(不会在数据库中创建字段)
查询示例:
post_1 = Post.objects.filter(id=1).first()
comment_list = post_1.comments.all()
Python contenttypes组件的更多相关文章
- Django contenttypes 组件
contenttypes组件 介绍 Django包含一个contenttypes应用程序(app),可以跟踪Django项目中安装的所有模型(Model),提供用于处理模型的高级通用接口. Conte ...
- Django contenttypes组件
contenttypes组件 介绍 Django包含一个contenttypes应用程序(app),可以跟踪Django项目中安装的所有模型(Model),提供用于处理模型的高级通用接口. Conte ...
- contenttypes组件 (处理大量外键)
介绍 Django contenttypes是一个非常有用的框架,主要用来创建模型间的通用关系(generic relation). https://www.cnblogs.com/huchong ...
- Django之contenttypes组件
目录 一. 介绍 二. 应用场景 1. 表结构设计的演变 2. GenericForeignKey和GenericRelation 3. 测试 一. 介绍 Django包含一个contenttypes ...
- Python标准组件ConfigParser配置文件解析器,保存配置时支持大写字母的方法
虽然自己已经改用xml作为配置文件首选格式了,但是有时候还是需要解析ini.cfg文件(为了兼容早期版本或者其他作者的软件). 基本上Python自带的ConfigParser足够应对了,但是美中不足 ...
- Python常用组件、命令大总结(持续更新)
Python开发常用组件.命令(干货) 持续更新中-关注公众号"轻松学编程"了解更多. 1.生成6位数字随机验证码 import random import string def ...
- Python分页组件
分页组件的实现: class Pagination(object): """ 自定义分页 """ def __init__(self,cur ...
- Python - Django - 组件
网站中通常会有一个导航条,如下图 这个导航条在很多页面都会存在 可以把导航条做成一个组件,让要显示导航条的网页包含 导航条组件 nav.html: <h1>假装这是一个导航条</h1 ...
- python 日志组件
日志组件: import logging import logging.handlers import os class logs(object): def __init__(self): self. ...
随机推荐
- nginx之location、rewrite配置
https://www.cnblogs.com/koal/p/6915106.html rewrite 地址重写 如上配置对于: /images/ttt/test.png 会重写到/mic?file ...
- 如何使用fastJson来解析JSON格式数据和生成JSON格式数据
由于项目用到了JSON格式的数据,在网上搜索到了阿里的fastjson比较好用,特此记录fastjson用法,以备以后查询之用. decode: 首先创建一个JSON解析类: public class ...
- 转 在shell脚本中使用expect实现scp传输问题
1.安装expect expect用于shell脚本中自动交互,其是基于tcl编程语言的工具.所以安装expect首先安装tcl.本文中使用的是expect5.45和tcl8.6.6. 安装tcl [ ...
- Bingding模型
public abstract class Binding : IDefaultCommunicationTimeouts public virtual IChannelListener<TCh ...
- bug 查找 (二) 从前端找到后端
bug 查找 (二) 从前端找到后端 几天来,组长说我们系统的 apm 数据不正确,最体表现就是前端项目这几天错误统计为 0. 这不正常(没有办法,我们代码写的很烂),因为前端环境很复杂,网络,浏览器 ...
- uvm_test——测试用例的起点
在UVM平台验证中,所有的test cases都extends uvm_test,首先,来看源代码 //------------------------------------------------ ...
- 《Python基础教程》 读书笔记 第九章 魔法方法、属性和迭代器(上)
构造方法 在Python中创建一个构造方法很容易.只要把init方法的名字从简单的init修改为魔法版本__init__即可: >>> class FooBar: ... d ...
- “chm 已取消到该网页的导航”解决方案
1. 右键单击该 CHM 文件,然后单击“属性”. 2. 单击“取消阻止”或者“解除锁定”. 3. 双击此 .chm 文件以打开此文件.
- (八)mybatis之映射器
映射器 映射器是由Java接口和XML文件(或注解)共同组成的,作用如下: ① 定义参数类型. ② 描述缓存. ③ 描述SQL语句. ④ 定义查询结果和POJO的映射关系. 以下用两种 ...
- Docker镜像的目录存储讲解
我们成功安装完docker后,执行命令行sudo docker run hello-world, 如果是第一次执行,则会从远程拉取hello-world的镜像到本地,然后运行,显示hello worl ...