Make sure your Django DEBUG setting is set to True. Then do this:

>>> from django.db import connection
>>> connection.queries
[{'sql': 'SELECT polls_polls.id, polls_polls.question, polls_polls.pub_date FROM polls_polls',
'time': '0.002'}]

connection.queries 只有在 DEBUG``True``时才可用. 它是按顺序执行的字典列表,每个字典包括:

``sql`` -- The raw SQL statement
``time`` -- How long the statement took to execute, in seconds.

connection.queries 包含了所有的SQL 语句 -- INSERT, UPDATES, SELECT, 等. 每次你的应用访问了数据库,查询都会被记录下来。

如果你在使用 multiple databases1,你可以对 connections 字典的每个成员使用相同的接口:

>>> from django.db import connections
>>> connections['my_db_alias'].queries

If you need to clear the query list manually at any point in your functions, call reset_queries(), like this:

from django.db import reset_queries

我可以让 Django 用一个之前已经存在的数据库吗?

是的,可以看这篇文档 导入已有数据库


看看Django对数据模型改动的支持:schema migrations (模型迁移)

如果你不介意清除数据,工程目录下的 manage.py 工具使用 flush 选项去立即重置数据库到 执行 migrate 后的状态

Django 支持多列主键吗?


But this isn't an issue in practice, because there's nothing stopping you from adding other constraints (using the unique_together model option or creating the constraint directly in your database), and enforcing the uniqueness at that level. Single-column primary keys are needed for things such as the admin interface to work; e.g., you need a single value to specify an object to edit or delete.

Django 支持 NoSQL 数据库吗?

NoSQL databases are not officially supported by Django itself. There are, however, a number of side projects and forks which allow NoSQL functionality in Django.

You can take a look on the wiki page which discusses some projects.

如何将特定于数据库的选项添加到我的 CREATE TABLE 语句中,例如将 MylSAM 指定为表类型?

我们尽量避免在 Django 代码中添加特例来容纳所有特定于数据库的选项,如表数据等。如果您想使用这些选项中的任何一个,请使用:class : ~ django.db.migrations.operations.RunSQL 操作来创建迁移,包含"ALTER TABLE"语句,它们可以执行您想要的操作。

例如,如果你使用 MySQL 并且你想让你的数据表使用 MyISAM 表类型,使用如下 SQL 语句:

Back to Top