将所有内容都放在 Django 模型中是否明智?
我刚刚继承了一个 Django 项目用于维护和持续开发。虽然我是一个相当熟练的程序员(也是 Python),但我几乎没有使用 Django 的经验,因此我需要对我的想法进行一些理智检查;)
当前的问题是:该项目包含一个自定义安装.sh 文件,它执行三件事:
- 创建一些非模型数据库并通过 SQL 导入初始数据
- 使用
manage.py
导入固定装置 - 常用的
migrate.pysyncdb
和migrate.py 迁移
。
(install.sh
还包含一些实现半生不熟的 south
依赖管理的逻辑,我将其替换为 native one)
我的想法如下:
- 为每个非模型数据库表生成模型(
manage.pyspectdb
for a开始,然后在应用程序中分开)。 - 将所有非
south
模型转换为south
- 将初始 SQL 数据转换为
south
固定装置 - 将数据库备份例程转换为
manage.py dumpdata (并恢复到
manage.py loaddata
固定装置)。 - 永远不要再使用原始 SQL
现在简单的问题是:这个计划是否明智?有哪些替代方案?
I've just inherited a Django project for maintenance and continuous development. While I'm a fairly proficient programmer (also Python) I have next to no experience with Django, therefore I need a bit of saneness-checking for my ideas ;)
The current problem is this: the project contains a custom install.sh
file, which does three things:
- Creating some non-model databases and importing initial data via SQL
- Importing fixtures using
manage.py
- The usual
migrate.py syncdb
andmigrate.py migrate
.
(install.sh
also contained some logic to implement half-baked south
dependency management, which I replaced by a native one)
My idea was the following:
- Generate models for every non-model database table (
manage.py inspectdb
for a start, split up in apps afterwards). - Convert all non-
south
models tosouth
- Convert initial SQL data to
south
fixtures - Convert database backup routines to
manage.py dumpdata
(and restoring tomanage.py loaddata
fixtures). - Never work with raw SQL again
Now the simple question is: is this plan sensible? What are the alternatives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您追求纯粹的非实际 SQL 路线,那么对我来说听起来很理智。
两个小点:
Sounds sane enough to me, if you're after a pure no-actual-SQL route.
Two small points:
听起来不错。不过,您可能希望在需要使用的基础上继续进行。从您立即需要的那些开始。
我不太明白非南方模型是什么。如果您的意思是为所有现有模型生成向南迁移(然后可能在迁移过程中伪造它们),那么是的,这是有道理的。
再次,什么是 South 固定装置?
我对此不太确定。我发现数据库特定的备份和恢复解决方案比
manage.py
使用得更频繁。特别是如果您有遗留触发器/存储过程等。理论上很好。请记住,您有时必须挖掘 SQL。只要您不以此作为与 SQL 失去联系的理由,或者以此为借口不让自己的双手“脏”在 SQL 上,那么就可以继续。
Sounds good. You may want to proceed on a need-to-use basis on this though. Start with those you need immediately.
I don't quite get what a non-south model is. If you mean generating south migrations for all existing models (and then probably
--fake
them during migration) then yes, this makes sense.Again, what are South fixtures?
I am not so sure about this one. I have seen database specific backup and restore solutions used more frequently than
manage.py
. Especially if you have legacy triggers/stored procedures etc.Good in theory. Do keep in mind that you will at some point have to dig up SQL. As long as you don't use this as a reason to lose touch with SQL, or use it as an excuse not to get your hands "dirty" with SQL, then yes, go ahead.