... 定されたフィクスチャに拡張子がある場合、そのタイプのフィクスチャだけが読み込まれます。たとえば 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 内の各 ... ... 定されたフィクスチャに拡張子がある場合、そのタイプのフィクスチャだけが読み込まれます。たとえば 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 を含む) に直接シリアライズした ... ... いレベルでは、次のようにデータをシリアライズできます。 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 ... ... 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 セッ ... ... 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 セッ ... ... 成には、 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 ... ... 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 ... module. 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 ... 2026年10月08日