| | 414 | Databases and models |
|---|
| | 415 | ==================== |
|---|
| | 416 | |
|---|
| | 417 | How can I see the raw SQL queries Django is running? |
|---|
| | 418 | ---------------------------------------------------- |
|---|
| | 419 | |
|---|
| | 420 | Make sure your Django ``DEBUG`` setting is set to ``True``. Then, just do |
|---|
| | 421 | this:: |
|---|
| | 422 | |
|---|
| | 423 | >>> from django.db import connection |
|---|
| | 424 | >>> connection.queries |
|---|
| | 425 | [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls', |
|---|
| | 426 | 'time': '0.002'}] |
|---|
| | 427 | |
|---|
| | 428 | ``connection.queries`` is only available if ``DEBUG`` is ``True``. It's a list |
|---|
| | 429 | of dictionaries in order of query execution. Each dictionary has the following:: |
|---|
| | 430 | |
|---|
| | 431 | ``sql`` -- The raw SQL statement |
|---|
| | 432 | ``time`` -- How long the statement took to execute, in seconds. |
|---|
| | 433 | |
|---|
| | 434 | ``connection.queries`` includes all SQL statements -- INSERTs, UPDATES, |
|---|
| | 435 | SELECTs, etc. Each time your app hits the database, the query will be recorded. |
|---|
| | 436 | |
|---|
| | 437 | Can I use Django with a pre-existing database? |
|---|
| | 438 | ---------------------------------------------- |
|---|
| | 439 | |
|---|
| | 440 | Yes. See `Integrating with a legacy database`_. |
|---|
| | 441 | |
|---|
| | 442 | .. _`Integrating with a legacy database`: http://www.djangoproject.com/documentation/legacy_databases/ |
|---|
| | 443 | |
|---|
| 442 | | The database API |
|---|
| 443 | | ================ |
|---|
| 444 | | |
|---|
| 445 | | How can I see the raw SQL queries Django is running? |
|---|
| 446 | | ---------------------------------------------------- |
|---|
| 447 | | |
|---|
| 448 | | Make sure your Django ``DEBUG`` setting is set to ``True``. Then, just do |
|---|
| 449 | | this:: |
|---|
| 450 | | |
|---|
| 451 | | >>> from django.db import connection |
|---|
| 452 | | >>> connection.queries |
|---|
| 453 | | [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls', |
|---|
| 454 | | 'time': '0.002'}] |
|---|
| 455 | | |
|---|
| 456 | | ``connection.queries`` is only available if ``DEBUG`` is ``True``. It's a list |
|---|
| 457 | | of dictionaries in order of query execution. Each dictionary has the following:: |
|---|
| 458 | | |
|---|
| 459 | | ``sql`` -- The raw SQL statement |
|---|
| 460 | | ``time`` -- How long the statement took to execute, in seconds. |
|---|
| 461 | | |
|---|
| 462 | | ``connection.queries`` includes all SQL statements -- INSERTs, UPDATES, |
|---|
| 463 | | SELECTs, etc. Each time your app hits the database, the query will be recorded. |
|---|
| 464 | | |
|---|
| 465 | | Can I use Django with a pre-existing database? |
|---|
| 466 | | ---------------------------------------------- |
|---|
| 467 | | |
|---|
| 468 | | Yes. See `Integrating with a legacy database`_. |
|---|
| 469 | | |
|---|
| 470 | | .. _`Integrating with a legacy database`: http://www.djangoproject.com/documentation/legacy_databases/ |
|---|
| | 472 | How do I add database-specific options to my CREATE TABLE statements, such as specifying MyISAM as the table type? |
|---|
| | 473 | ------------------------------------------------------------------------------------------------------------------ |
|---|
| | 474 | |
|---|
| | 475 | We try to avoid adding special cases in the Django code to accomodate all the |
|---|
| | 476 | database-specific options such as table type, etc. If you'd like to use any of |
|---|
| | 477 | these options, create an `SQL initial data file`_ that contains ``ALTER TABLE`` |
|---|
| | 478 | statements that do what you want to do. The initial data files are executed in |
|---|
| | 479 | your database after the ``CREATE TABLE`` statements. |
|---|
| | 480 | |
|---|
| | 481 | For example, if you're using MySQL and want your tables to use the MyISAM table |
|---|
| | 482 | type, create an initial data file and put something like this in it:: |
|---|
| | 483 | |
|---|
| | 484 | ALTER TABLE myapp_mytable ENGINE=MyISAM; |
|---|
| | 485 | |
|---|
| | 486 | As explained in the `SQL initial data file`_ documentation, this SQL file can |
|---|
| | 487 | contain arbitrary SQL, so you can make any sorts of changes you need to make. |
|---|
| | 488 | |
|---|
| | 489 | .. _SQL initial data file: http://www.djangoproject.com/documentation/model_api/#providing-initial-sql-data |
|---|