PageRenderTime 147ms CodeModel.GetById 143ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/faq/models.txt

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