我正在启动一个项目,并决定使用 Django。
我的问题是关于数据库的创建。我已经阅读了教程和一些书籍,这些书籍总是开始创建模型,然后同步数据库。我不得不说,这对我来说有点奇怪。我总是从数据库开始,定义模式,然后创建我的数据库抽象(模型、实体等)。
我检查了一些外部可插入应用程序,它们也使用“模型优先”实践。
我可以看到“模型优先”方法的一些优点,例如可移植性、重新部署等。
但我也看到一些缺点:如何创建索引、索引的类型、触发器、视图、SP 等。
所以,你如何开始一个现实生活中的项目?
I'm starting a project and i've decided to use Django.
My question is regarding to the creation of the database. I've read the tutorial and some books and those always start creating the models, and then synchronizing the DataBase. I've to say, that's a little weird for me. I've always started with the DB, defining the schema, and after that creating my DB Abstractions (models, entities, etc).
I've check some external-pluggable apps and those use that "model first" practice too.
I can see some advantages for the "model-first" approach, like portability, re-deployment, etc.
But i also see some disadvantages: how to create indexes, the kind of index, triggers, views, SPs, etc.
So, How do you start a real life project?
发布评论
评论(2)
触发器、视图和存储过程实际上并不是 Django 世界的一部分。可以使用它们,但这是痛苦且不必要的。 Django 的开发人员认为业务逻辑属于 Python,而不是数据库。
至于索引,您可以将它们与您的模型一起创建(使用诸如
db_index
和unique_together
之类的东西,或者您可以稍后使用诸如 South 之类的东西通过数据库迁移添加它们。Triggers, views, and stored procedures aren't really a part of the Django world. It can be made to use them, but it's painful and unnecessary. Django's developers take the view that business logic belongs in Python, not in your database.
As for indexes, you can create them along with your models (with things like
db_index
andunique_together
, or you can add them later via database migrations using something like South.大多数情况下,我们不会为我们的模型编写 SQL(例如创建索引、创建表等),而是依靠 Django 为我们生成它。
从模型层开始设计应用程序是绝对可以的,因为您可以依靠 Django 生成您所需的正确数据库 sql。
但是,Django 确实提供了各种函数供您复制这些数据库功能:
db_index=True
唯一约束指定unique = True
或unique_togther = (('field1', field2'),)
用于复合唯一约束。使用 Django 而不是编写 sql 的优点是您可以将自己从正在使用的特定数据库中抽象出来。换句话说,您可能有一天使用
SQLite
,第二天切换到PostgresQL
或MySQL
,而且这种更改相对轻松。示例:
当您运行此命令时:
Django 自动创建表、索引、触发器等,它需要支持您创建的模型。如果您不习惯 Django 为您创建数据库,您可以随时使用:
这将打印 Django 需要的 SQL 语句,以便其模型正常运行。还有其他 sql 命令供您使用:
请参阅:https://docs.djangoproject.com/en/1.3/ref/django-admin/#sql-appname-appname
Mostly we don't write SQL (e.g. create index, create tables, etc...) for our models, and instead rely on Django to generate it for us.
It is absolutely okay to start with designing your app from the model layer, as you can rely on Django to generate the correct database sql needed for you.
However, Django does provide various functions for you to replicate these database functions:
db_index=True
unique = True
orunique_togther = (('field1', field2'),)
for composite unique constraint.The advantage with using Django instead of writing sql is that you abstract yourself away from the particular database you are using. In other words, you could be on
SQLite
one day and switch toPostgresQL
orMySQL
the next and the change would be relatively painless.Example:
When you run this:
Django automatically creates the tables, indexes, triggers, etc, it needs to support the models you've created. If you are not comfortable with django creating your database for you, you can always use:
This will print out the SQL statements that Django would need to have in order for its models to function properly. There are other
sql
commands for you to use:see: https://docs.djangoproject.com/en/1.3/ref/django-admin/#sql-appname-appname