在Django中对数据进行非规范化的最佳方法? [英] Best way to denormalize data in Django?

查看:49
本文介绍了在Django中对数据进行非规范化的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的Web应用程序,并且存储一些非规范化数据非常有意义.

I'm developing a simple web app, and it makes a lot of sense to store some denormalized data.

想象一下一个跟踪评论的博客平台,并且BlogEntry模型具有一个"CommentCount"字段,我想保持最新状态.

Imagine a blogging platform that keeps track of Comments, and the BlogEntry model has a "CommentCount" field that I'd like to keep up to date.

执行此操作的一种方法是使用Django信号.

One way of doing this would be to use Django signals.

执行此操作的另一种方法是将钩子直接放置在我的代码中,该代码创建和取消注释对象,以在BlogEntry上同步调用某些方法来增加/减少注释计数.

Another way of doing this would be to put hooks directly in my code that creates and destrys Comment objects to synchronously call some methods on BlogEntry to increment/decrement the comment count.

我想还有其他用装饰器或其他伏都教徒完成此操作的Python方法.

I suppose there are other pythonic ways of accomplishing this with decorators or some other voodoo.

在Django中进行非规范化的标准设计模式是什么?实际上,如果发生错误,您还必须编写一致性检查程序和数据修复程序吗?

What is the standard Design Pattern for denormalizing in Django? In practice, do you also have to write consistency checkers and data fixers in case of errors?

推荐答案

您在Django中有管理员.

You have managers in Django.

使用定制的经理来创建和维护FK关系.

Use a customized manager to do creates and maintain the FK relationships.

管理者可以在更新子集时更新计数.

The manager can update the counts as the sets of children are updated.

如果您不想创建自定义的经理,只需扩展save方法.您想要对计数和总和进行非规格化的所有操作都可以在save中完成.

If you don't want to make customized managers, just extend the save method. Everything you want to do for denormalizing counts and sums can be done in save.

您不需要信号.只需扩展save.

You don't need signals. Just extend save.

这篇关于在Django中对数据进行非规范化的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆