models.txt 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. .. _faq-models:
  2. FAQ: Databases and models
  3. =========================
  4. How can I see the raw SQL queries Django is running?
  5. ----------------------------------------------------
  6. Make sure your Django ``DEBUG`` setting is set to ``True``. Then, just do
  7. this::
  8. >>> from django.db import connection
  9. >>> connection.queries
  10. [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls',
  11. 'time': '0.002'}]
  12. ``connection.queries`` is only available if ``DEBUG`` is ``True``. It's a list
  13. of dictionaries in order of query execution. Each dictionary has the following::
  14. ``sql`` -- The raw SQL statement
  15. ``time`` -- How long the statement took to execute, in seconds.
  16. ``connection.queries`` includes all SQL statements -- INSERTs, UPDATES,
  17. SELECTs, etc. Each time your app hits the database, the query will be recorded.
  18. Can I use Django with a pre-existing database?
  19. ----------------------------------------------
  20. Yes. See :ref:`Integrating with a legacy database <howto-legacy-databases>`.
  21. If I make changes to a model, how do I update the database?
  22. -----------------------------------------------------------
  23. If you don't mind clearing data, your project's ``manage.py`` utility has an
  24. option to reset the SQL for a particular application::
  25. manage.py reset appname
  26. This drops any tables associated with ``appname`` and recreates them.
  27. If you do care about deleting data, you'll have to execute the ``ALTER TABLE``
  28. statements manually in your database. That's the way we've always done it,
  29. because dealing with data is a very sensitive operation that we've wanted to
  30. avoid automating. That said, there's some work being done to add partially
  31. automated database-upgrade functionality.
  32. Do Django models support multiple-column primary keys?
  33. ------------------------------------------------------
  34. No. Only single-column primary keys are supported.
  35. But this isn't an issue in practice, because there's nothing stopping you from
  36. adding other constraints (using the ``unique_together`` model option or
  37. creating the constraint directly in your database), and enforcing the
  38. uniqueness at that level. Single-column primary keys are needed for things such
  39. as the admin interface to work; e.g., you need a simple way of being able to
  40. specify an object to edit or delete.
  41. How do I add database-specific options to my CREATE TABLE statements, such as specifying MyISAM as the table type?
  42. ------------------------------------------------------------------------------------------------------------------
  43. We try to avoid adding special cases in the Django code to accommodate all the
  44. database-specific options such as table type, etc. If you'd like to use any of
  45. these options, create an :ref:`SQL initial data file <initial-sql>` that
  46. contains ``ALTER TABLE`` statements that do what you want to do. The initial
  47. data files are executed in your database after the ``CREATE TABLE`` statements.
  48. For example, if you're using MySQL and want your tables to use the MyISAM table
  49. type, create an initial data file and put something like this in it::
  50. ALTER TABLE myapp_mytable ENGINE=MyISAM;
  51. As explained in the :ref:`SQL initial data file <initial-sql>` documentation,
  52. this SQL file can contain arbitrary SQL, so you can make any sorts of changes
  53. you need to make.
  54. Why is Django leaking memory?
  55. -----------------------------
  56. Django isn't known to leak memory. If you find your Django processes are
  57. allocating more and more memory, with no sign of releasing it, check to make
  58. sure your ``DEBUG`` setting is set to ``False``. If ``DEBUG`` is ``True``, then
  59. Django saves a copy of every SQL statement it has executed.
  60. (The queries are saved in ``django.db.connection.queries``. See
  61. `How can I see the raw SQL queries Django is running?`_.)
  62. To fix the problem, set ``DEBUG`` to ``False``.
  63. If you need to clear the query list manually at any point in your functions,
  64. just call ``reset_queries()``, like this::
  65. from django import db
  66. db.reset_queries()