... 定されたフィクスチャに拡張子がある場合、そのタイプのフィクスチャだけが読み込まれます。たとえば django-admin loaddata mydata.json mydata と呼ばれる JSON フィクスチャのみを読み込みます。フィクスチャの拡張子は シリアライザ に登録された名前 (例: json や x ... go-admin loaddata mydata これは mydata というフィクスチャタイプのフィクスチャを探します。フィクスチャディレクトリに mydata.json が含まれていれば、そのフィクスチャは JSON フィクスチャとして読み込まれます。 命名されたフィクスチャはディレクトリ・コンポー ... ネントを含むことができます。これらのディレクトリは検索パスに含まれます。たとえば: django-admin loaddata foo/bar/mydata.json これは、インストールされている各アプリケーションに対して <app_label>/fixtures/foo/bar/mydata.json を、 FIXTURE_DIRS 内の各 ... ... いレベルでは、次のようにデータをシリアライズできます。 from django.core import serializers data = serializers . serialize ( "json" , SomeModel . objects . all ()) serialize 関数の引数は、データをシリアライズするフォーマット ( シリアライズフォーマット ) ... す。) django.core.serializers. get_serializer ( format ) シリアライザーオブジェクトを次のように直接使用することもできます。 JSONSerializer = serializers . get_serializer ( "json" ) json_serializer = JSONSerializer () json_serializer . serialize ( quer ... yset ) data = json_serializer . getvalue () これは次のようにデータをファイルライクなオブジェクト ( HttpResponse を含む) に直接シリアライズした ... ... response to return something not rendered via a Django template then you'll want to do it. For an example of this, see the JSONResponseMixin example . render_to_response() 自体は get_template_names() を呼び出しますが、デフォルトではクラスベースのビュ ... に立つのは、同じことを何度もしたい場合です。たとえば API を作成している場合、全てのビューはレンダリングされた HTML ではなく JSON を返す必要があります。 全てのビューで利用するために、 JSONへの変換処理をするmixinクラスを作成できます。 たとえば、JSONのmi ... xinは以下のようになるでしょう。 from django.http import JsonResponse class JSONResponseMixin : """ A mixin that can be used to render a JSON response. """ def render_to_json_response ... ... diately escalates to a remote code execution vulnerability. This attack can be mitigated by serializing session data using JSON rather than pickle . To facilitate this, Django 1.5.3 introduces a new setting, SESSION_SERIALIZER , to customize the sess ... ion serialization format. For backwards compatibility, this setting defaults to using pickle . While JSON serialization does not support all Python objects like pickle does, we highly recommend switching to JSON-serialized value ... s. Also, as JSON requires string keys, you will likely run into problems if you are using non-string keys in request.session . See the セッ ... ... P methods aren't implemented by web browsers. Rather, they're used in APIs, which transfer data in various formats such as JSON or XML. Since such requests may contain arbitrary data, Django doesn't attempt to decode their body. However, the test cli ... you must encode your data before passing it to the test client and set the content_type argument. System version of simplejson no longer used As explained below , Django 1.5 deprecates django.utils.simplejson in favor of Python 2.6's built-in json m ... odule. In theory, this change is harmless. Unfortunately, because of incompatibilities between versions of simplejson , it may trigger errors in some circumstances. JSON-related features in Django 1.4 always used django.utils.simplejson . T ... ... aenqueue() is available as an async variant of enqueue() . Because both Task arguments and return values are serialized to JSON, they must be JSON-serializable: >>> process_data . enqueue ( datetime . now ()) Traceback (most recent call last): ... Ty ... peError : Object of type datetime is not JSON serializable Arguments must also be able to round-trip through a json.dumps() / json.loads() cycle without changing type. ... ceback (most recent call last): ... TypeError : unhashable type: 'list' The double_dictionary Task fails because after the JSON round-trip the tuple (1, 2, 3) becomes the list [1, 2, 3] , which cannot be used as a dictionary key. In general, complex ... ... 成には、 django.test.Client クラスを使います。 class Client ( enforce_csrf_checks = False , raise_request_exception = True , json_encoder = DjangoJSONEncoder , * , headers = None , query_params = None , ** defaults ) [ソース] テスト用の HTTP クライアン ... ion 引数を指定することで、リクエスト中に発生した例外をテストでも発生させるかどうかを制御できます。デフォルトは True です。 json_encoder 引数を指定すると、 post() に記述されているJSONシリアライズ用のカスタムJSONエンコーダを指定できます。 Client インス ... ( ... "/customers/details/" , ... query_params = { "name" : "fred" , "age" : 7 }, ... headers = { "accept" : "application/json" }, ... ) ...will send the HTTP header HTTP_ACCEPT to the details view, which is a good way to test code paths that use th ... ... peed regressions for your existing, synchronous code. It should have no noticeable effect on any existing Django projects. JSONField for all supported database backends Django now includes models.JSONField and forms.JSONField that can be used on all ... supported database backends. Both fields support the use of custom JSON encoders and decoders. The model field supports the introspection, lookups, and transforms that were previously PostgreSQL ... -only: from django.db import models class ContactInfo ( models . Model ): data = models . JSONField () ContactInfo . objects . create ( data = { "name" : "John" , "cities" : [ "London" , "Cambridge" ], "pets" : { "dog ... ... の同期関数の中に記述し、 sync_to_async を使用して呼び出すことをおすすめします。詳しくは 非同期サポート を参照してください。 JSONField へのクエリ JSONField では、ルックアップの実装は主にキーのトランスフォーム(変換)の存在によって異なります。デモンスト ... from django.db import models class Dog ( models . Model ): name = models . CharField ( max_length = 200 ) data = models . JSONField ( null = True ) def __str__ ( self ): return self . name None の保存とクエリ As with other fields, storing None as th ... e field's value will store it as SQL NULL . While not recommended, it is possible to store JSON scalar null instead of SQL NULL by using the JSONNull() expression. どちらの値が格納されていても、データベースから取得した ... ... が指定されたいずれのタイプも受け入れない場合は None を返します。 クライアントが Accept ヘッダーとして text/html,application/json;q=0.8 を送信したと仮定します。 >>> request . get_preferred_type ([ "text/html" , "application/json" ]) "text/html" >>> req ... uest . get_preferred_type ([ "application/json" , "text/plain" ]) "application/json" >>> request . get_preferred_type ([ "application/xml" , "text/plain" ]) None MIME タ ... s from django.http import HttpResponse class HttpResponseNoContent ( HttpResponse ): status_code = HTTPStatus . NO_CONTENT JsonResponse オブジェクト class JsonResponse ( data , encoder = DjangoJSONEncoder , safe = True , json_dumps_params = None , ** ... 2026年10月08日