... のデータベースコネクションを表します。データベースコネクションを利用するには、カーソルオブジェクトを取得するため connection.cursor() を呼び出してください。そして、 cursor.execute(sql, [params]) を呼び出して SQL を実行した後、 cursor.fetchone() もしくは ... ursor.fetchall() が結果の行を返します。 例: from django.db import connection def my_custom_sql ( self ): with connection . cursor () as cursor : cursor . execute ( "UPDATE bar SET foo = 1 WHERE baz = %s " , [ self . baz ]) cursor . execute ( "SELECT fo ... o FROM bar WHERE baz = %s " , [ self . baz ]) row = cursor . fetchone () return row SQLインジェクションから保護するために、SQL文字列の %s プレースホルダの前後に引用符を含めてはいけま ... ... ostgreSQL prevents running Django with PgBouncer in transaction pooling mode. To reallow that, use the DISABLE_SERVER_SIDE_CURSORS setting in DATABASES . See トランザクションプールとサーバーサイドカーソル for more discussion. バグ修正 Made migrations r ... #28059 ). Fixed crash in BaseGeometryWidget.subwidgets() ( #28039 ). Fixed exception reraising in ORM query execution when cursor.execute() fails and the subsequent cursor.close() also fails ( #28091 ). Fixed a regression where CheckboxSelectMultiple , ... ... ix (which returns FormMixin.prefix by default) to allow customizing the prefix of the form. Raw queries ( Manager.raw() or cursor.execute() ) can now use the "pyformat" parameter style, where placeholders in the query are given as '%(name)s' and the pa ... on Oracle, an exception is generated. No database change is needed for SQLite or PostgreSQL databases. Percent literals in cursor.execute queries When you are running raw SQL queries through the cursor.execute method, the rule about doubling percent li ... ral percent characters if you are also providing replacement parameters. For example: # No parameters, no percent doubling cursor . execute ( "SELECT foo FROM bar WHERE baz = '30%'" ) # Parameters passed, non-placeholders have to be doubled cursor . ex ... ... it was None on databases that support time zones. connection._nodb_connection property is changed to the connection._nodb_cursor() method and now returns a context manager that yields a cursor and automatically closes the cursor and connection upon ex ... ... ser and server time zone are different, to clarify how the value inserted in the field will be interpreted. Using database cursors as context managers Prior to Python 2.7, database cursors could be used as a context manager. The specific backend's curs ... or defined the behavior of the context manager. The behavior of magic method lookups was changed with Python 2.7 and cursors were no longer usable as context managers. Django 1.7 allows a cursor to be used as a context manager. That is, the follo ... wing can be used: with connection . cursor () as c : c . execute ( ... ) 代わりに、次のようにします: c = connection . cursor () try : c . execute ( ... ) finally : c ... ... icInlineModelAdmin ( #23539 ). Made migrations.RunSQL no longer require percent sign escaping. This is now consistent with cursor.execute() ( #23426 ). TEST 辞書の SERIALIZE 項目を使用可能にしました ( #23421 )。 Fixed bug in migrations that prevented f ... ... 対してグループ化されるリグレッションを修正しました ( #34464 )。 Django 4.2 のリグレッションに従い、PostgreSQL の OPTIONS で "cursor_factory" オプションを設定することを許可しました ( #34466 )。 Django 4.2のリグレッション ( #34470 ) に対処し、PostgreSQL の ... ... c >>> from django.db import connection >>> # In an async context so you cannot use the database directly: >>> connection . cursor () django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_a ... sync. >>> # Nor can you pass resolved connection attributes across threads: >>> await sync_to_async ( connection . cursor )() django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The o ... ... nnections dictionary referencing those objects is still thread-local. Therefore if you just use the ORM or DatabaseWrapper.cursor() then the behavior is still the same as before. Note, however, that django.db.connection does not directly reference the ... g thread-sharing by default, so this does not affect any existing code that purely relies on the ORM or on DatabaseWrapper.cursor() . Finally, while it's now possible to pass connections between threads, Django doesn't make any effort to synchronize ac ... ... ように、エイリアスを使って個別の接続を取得できます。 from django.db import connections with connections [ "my_db_alias" ] . cursor () as cursor : ... 複数データベースの制約 データベースをまたぐリレーション Djangoは現在、複数のデータベースにまたがる外部キ ... 2026年10月08日