... 行するために、 GeoIP2 オブジェクトには、バイナリ形式の GeoIP Country および City データセットと、 geoip2 Python パッケージが必要です (CSV ファイルは使えません!)。これらは、例えば MaxMind や D ... B-IP のウェブサイトからダウンロードできます。 GeoLite2-Country.mmdb.gz および GeoLite2-City.mmdb.gz ファイルを取得し、これらを GEOIP_PATH 設定に対応するディレクトリに解凍してください。 ... めします。 カスタマイズ例 ¶ 以下はその使用例です: >>> from django.contrib.gis.geoip2 import GeoIP2 >>> g = GeoIP2 () >>> g . country ( "google.com" ) {'continent_code': 'NA', 'continent_name': 'North America', 'country_code': 'US', 'country_name': 'United ... ... 行するために、 GeoIP2 オブジェクトには、バイナリ形式の GeoIP Country および City データセットと、 geoip2 Python パッケージが必要です (CSV ファイルは使えません!)。これらは、例えば MaxMind や D ... B-IP のウェブサイトからダウンロードできます。 GeoLite2-Country.mmdb.gz および GeoLite2-City.mmdb.gz ファイルを取得し、これらを GEOIP_PATH 設定に対応するディレクトリに解凍してください。 ... めします。 カスタマイズ例 ¶ 以下はその使用例です: >>> from django.contrib.gis.geoip2 import GeoIP2 >>> g = GeoIP2 () >>> g . country ( "google.com" ) {'continent_code': 'NA', 'continent_name': 'North America', 'country_code': 'US', 'country_name': 'United ... ... ブジェクトのグループを作ります。 この複雑なタグは例を通して説明するのがいいでしょう: cities は "name" , "population" , and "country" をキーとして含むディクショナリによって表された都市のリストとします: cities = [ { "name" : "Mumbai" , "population" : "19,0 ... 00,000" , "country" : "India" }, { "name" : "Calcutta" , "population" : "15,000,000" , "country" : "India" }, { "name" : "New York" , "popula ... tion" : "20,000,000" , "country" : "USA" }, { "name" : "Chicago" , "population" : "7,000,000" , "country" : "USA" }, { "name" : "Tokyo" , "population" : " ... ... ブジェクトのグループを作ります。 この複雑なタグは例を通して説明するのがいいでしょう: cities は "name" , "population" , and "country" をキーとして含むディクショナリによって表された都市のリストとします: cities = [ { "name" : "Mumbai" , "population" : "19,0 ... 00,000" , "country" : "India" }, { "name" : "Calcutta" , "population" : "15,000,000" , "country" : "India" }, { "name" : "New York" , "popula ... tion" : "20,000,000" , "country" : "USA" }, { "name" : "Chicago" , "population" : "7,000,000" , "country" : "USA" }, { "name" : "Tokyo" , "population" : " ... ... ンファイルの Migration クラスから独立した別の関数として記述し、 RunPython に渡すことを推奨します。以下に RunPython を用いて Country モデル上の初期オブジェクトを追加する例を示します: from django.db import migrations def forwards_func ( apps , schema_edito ... r ): # We get the model from the versioned app registry; # if we directly import it, it'll be the wrong version Country = apps . get_model ( "myapp" , "Country" ) db_alias = schema_editor . connection . alias Country . objects . using ( db_al ... ias ) . bulk_create ( [ Country ( name = "USA" , code = "us" ), Country ( name = "France" , code = "fr" ), ] ) def reverse_func ( apps , schema_editor ): ... ... ンファイルの Migration クラスから独立した別の関数として記述し、 RunPython に渡すことを推奨します。以下に RunPython を用いて Country モデル上の初期オブジェクトを追加する例を示します: from django.db import migrations def forwards_func ( apps , schema_edito ... r ): # We get the model from the versioned app registry; # if we directly import it, it'll be the wrong version Country = apps . get_model ( "myapp" , "Country" ) db_alias = schema_editor . connection . alias Country . objects . using ( db_al ... ias ) . bulk_create ( [ Country ( name = "USA" , code = "us" ), Country ( name = "France" , code = "fr" ), ] ) def reverse_func ( apps , schema_editor ): ... ... 違いは、フォームフィールドの選択肢を入力するためにデータベースクエリを作成することです: class LocationForm ( forms . Form ): country = forms . ChoiceField ( choices = [ c . name for c in Country . objects . all ()]) 上の例では、 QuerySet がイテレートされる ... ため、 Country.objects.all() からのクエリがモジュールのインポート時に実行されます。警告を回避するには、代わりに ModelChoiceField を使用し ... ます: class LocationForm ( forms . Form ): country = forms . ModelChoiceField ( queryset = Country . objects . all ()) この警告を引き起こしたコードを見つけやすくするために、 ... ... 違いは、フォームフィールドの選択肢を入力するためにデータベースクエリを作成することです: class LocationForm ( forms . Form ): country = forms . ChoiceField ( choices = [ c . name for c in Country . objects . all ()]) 上の例では、 QuerySet がイテレートされる ... ため、 Country.objects.all() からのクエリがモジュールのインポート時に実行されます。警告を回避するには、代わりに ModelChoiceField を使用し ... ます: class LocationForm ( forms . Form ): country = forms . ModelChoiceField ( queryset = Country . objects . all ()) この警告を引き起こしたコードを見つけやすくするために、 ... ... 標に関する追加情報にアクセスしたい時は、次のように実装します。 class GPSCoordinatesBoundField ( BoundField ): @property def country ( self ): """ Return the country the coordinates lie in or None if it can't be determined. """ value = self . value () if ... value : return get_country_from_coordinates ( value ) else : return None class GPSCoordinatesField ( Field ): bound_field_class = GPSCoordinatesBound ... Field これで、テンプレート内で {{ form.coordinates.country }} を使って国にアクセスできるようになりました。 デフォルトのフォームフィールドのテンプレートレンダリングをカスタマイズした ... ... 標に関する追加情報にアクセスしたい時は、次のように実装します。 class GPSCoordinatesBoundField ( BoundField ): @property def country ( self ): """ Return the country the coordinates lie in or None if it can't be determined. """ value = self . value () if ... value : return get_country_from_coordinates ( value ) else : return None class GPSCoordinatesField ( Field ): bound_field_class = GPSCoordinatesBound ... Field これで、テンプレート内で {{ form.coordinates.country }} を使って国にアクセスできるようになりました。 デフォルトのフォームフィールドのテンプレートレンダリングをカスタマイズした ... 2026年10月08日