{"title":"GeoDjango 模型 API","version":"6.1","locale":"zh-hans","docname":"ref/contrib/gis/model-api","url":"/zh-hans/6.1/ref/contrib/gis/model-api/","canonical":"https://djangodocs.dev/zh-hans/6.1/ref/contrib/gis/model-api/","summary":"This document explores the details of the GeoDjango Model API. Throughout this section, we'll be using the following geographic model of a ZIP code and of a Digital…","html":"<span id=\"geodjango-model-api\"></span><h1>GeoDjango 模型 API<a class=\"heading-anchor\" href=\"#module-django.contrib.gis.db.models\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This document explores the details of the GeoDjango Model API. Throughout this\nsection, we'll be using the following geographic model of a <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/ZIP_code\">ZIP code</a> and\nof a <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Digital_elevation_model\">Digital Elevation Model</a> as our examples:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.gis.db</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">models</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Zipcode</span><span class=\"p\">(</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">Model</span><span class=\"p\">):</span>\n    <span class=\"n\">code</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">max_length</span><span class=\"o\">=</span><span class=\"mi\">5</span><span class=\"p\">)</span>\n    <span class=\"n\">poly</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">PolygonField</span><span class=\"p\">()</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Elevation</span><span class=\"p\">(</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">Model</span><span class=\"p\">):</span>\n    <span class=\"n\">name</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">max_length</span><span class=\"o\">=</span><span class=\"mi\">100</span><span class=\"p\">)</span>\n    <span class=\"n\">rast</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">RasterField</span><span class=\"p\">()</span>\n</code></pre></div>\n<section id=\"spatial-field-types\">\n<h2>空间字段类型<a class=\"heading-anchor\" href=\"#spatial-field-types\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>空间字段包括一系列几何字段类型和一个栅格字段类型。每种几何字段类型对应于 OpenGIS Simple Features 规范 <a class=\"footnote-reference brackets\" href=\"#fnogc\" id=\"id3\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>1<span class=\"fn-bracket\">]</span></a>。对于栅格数据，没有类似的标准。</p>\n<section id=\"geometryfield\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">GeometryField</span></code><a class=\"heading-anchor\" href=\"#geometryfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.GeometryField\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">GeometryField</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.GeometryField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>几何字段的基类。</p>\n</section>\n<section id=\"pointfield\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">PointField</span></code><a class=\"heading-anchor\" href=\"#pointfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.PointField\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">PointField</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.PointField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>存储一个 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geos/#django.contrib.gis.geos.Point\" title=\"django.contrib.gis.geos.Point\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Point</span></code></a>。</p>\n</section>\n<section id=\"linestringfield\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">LineStringField</span></code><a class=\"heading-anchor\" href=\"#linestringfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.LineStringField\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">LineStringField</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.LineStringField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>存储一个 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geos/#django.contrib.gis.geos.LineString\" title=\"django.contrib.gis.geos.LineString\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">LineString</span></code></a>。</p>\n</section>\n<section id=\"polygonfield\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">PolygonField</span></code><a class=\"heading-anchor\" href=\"#polygonfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.PolygonField\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">PolygonField</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.PolygonField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>存储一个 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geos/#django.contrib.gis.geos.Polygon\" title=\"django.contrib.gis.geos.Polygon\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Polygon</span></code></a>。</p>\n</section>\n<section id=\"multipointfield\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">MultiPointField</span></code><a class=\"heading-anchor\" href=\"#multipointfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.MultiPointField\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">MultiPointField</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.MultiPointField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>存储一个 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geos/#django.contrib.gis.geos.MultiPoint\" title=\"django.contrib.gis.geos.MultiPoint\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MultiPoint</span></code></a>。</p>\n</section>\n<section id=\"multilinestringfield\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">MultiLineStringField</span></code><a class=\"heading-anchor\" href=\"#multilinestringfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.MultiLineStringField\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">MultiLineStringField</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.MultiLineStringField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>存储一个 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geos/#django.contrib.gis.geos.MultiLineString\" title=\"django.contrib.gis.geos.MultiLineString\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MultiLineString</span></code></a>。</p>\n</section>\n<section id=\"multipolygonfield\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">MultiPolygonField</span></code><a class=\"heading-anchor\" href=\"#multipolygonfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.MultiPolygonField\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">MultiPolygonField</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.MultiPolygonField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>存储一个 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geos/#django.contrib.gis.geos.MultiPolygon\" title=\"django.contrib.gis.geos.MultiPolygon\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MultiPolygon</span></code></a>。</p>\n</section>\n<section id=\"geometrycollectionfield\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">GeometryCollectionField</span></code><a class=\"heading-anchor\" href=\"#geometrycollectionfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.GeometryCollectionField\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">GeometryCollectionField</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.GeometryCollectionField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>存储一个 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geos/#django.contrib.gis.geos.GeometryCollection\" title=\"django.contrib.gis.geos.GeometryCollection\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GeometryCollection</span></code></a>。</p>\n</section>\n<section id=\"rasterfield\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">RasterField</span></code><a class=\"heading-anchor\" href=\"#rasterfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.RasterField\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">RasterField</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.RasterField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>存储一个 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/gdal/#django.contrib.gis.gdal.GDALRaster\" title=\"django.contrib.gis.gdal.GDALRaster\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GDALRaster</span></code></a>。</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">RasterField</span></code> 目前仅在 PostGIS 后端实现。</p>\n</section>\n</section>\n<section id=\"spatial-field-options\">\n<h2>空间字段选项<a class=\"heading-anchor\" href=\"#spatial-field-options\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>除了 Django 模型字段可用的常规 <span class=\"xref std std-ref\">常见模型字段选项</span> 外，空间字段还有以下附加选项。所有这些选项都是可选的。</p>\n<section id=\"srid\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">srid</span></code><a class=\"heading-anchor\" href=\"#srid\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.BaseSpatialField.srid\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseSpatialField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">srid</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.BaseSpatialField.srid\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>将几何字段的 SRID <a class=\"footnote-reference brackets\" href=\"#fnogcsrid\" id=\"id4\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>2<span class=\"fn-bracket\">]</span></a> （空间参考系统标识）设置为给定值。默认值为 4326（也称为 <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/WGS84\">WGS84</a>，单位为经度和纬度的度数）。</p>\n<section id=\"selecting-an-srid\">\n<span id=\"id6\"></span><h4>选择一个 SRID<a class=\"heading-anchor\" href=\"#selecting-an-srid\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Choosing an appropriate SRID for your model is an important decision that the\ndeveloper should consider carefully. The SRID is an integer specifier that\ncorresponds to the projection system that will be used to interpret the data in\nthe spatial database. <a class=\"footnote-reference brackets\" href=\"#fnsrid\" id=\"id7\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>3<span class=\"fn-bracket\">]</span></a>  Projection systems give the context to the\ncoordinates that specify a location. Although the details of <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Geodesy\">geodesy</a> are\nbeyond the scope of this documentation, the general problem is that the earth\nis spherical and representations of the earth (e.g., paper maps, web maps) are\nnot.</p>\n<p>Most people are familiar with using latitude and longitude to reference a\nlocation on the earth's surface. However, latitude and longitude are angles,\nnot distances. In other words, while the shortest path between two points on a\nflat surface is a straight line, the shortest path between two points on a\ncurved surface (such as the earth) is an <em>arc</em> of a <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Great_circle\">great circle</a>.\n<a class=\"footnote-reference brackets\" href=\"#fnthematic\" id=\"id8\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>4<span class=\"fn-bracket\">]</span></a></p>\n<p>Thus, additional computation is required to obtain distances in planar units\n(e.g., kilometers and miles). Using a geographic coordinate system may\nintroduce complications for the developer later on. For example, SpatiaLite\ndoes not have the capability to perform distance calculations between\ngeometries using geographic coordinate systems, e.g. constructing a query to\nfind all points within 5 miles of a county boundary stored as WGS84. <a class=\"footnote-reference brackets\" href=\"#fndist\" id=\"id9\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>5<span class=\"fn-bracket\">]</span></a></p>\n<p>Portions of the earth's surface may projected onto a two-dimensional, or\nCartesian, plane. Projected coordinate systems are especially convenient for\nregion-specific applications, e.g., if you know that your database will only\ncover geometries in <a class=\"reference external\" href=\"https://spatialreference.org/ref/epsg/2796/\">North Kansas</a>, then you may consider using projection\nsystem specific to that region. Moreover, projected coordinate systems are\ndefined in Cartesian units (such as meters or feet), easing distance\ncalculations.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>如果您希望在 PostGIS 中使用 WGS84 中的非点几何体执行任意距离查询，并且希望获得良好的性能，请启用 <a class=\"reference internal\" href=\"#django.contrib.gis.db.models.GeometryField.geography\" title=\"django.contrib.gis.db.models.GeometryField.geography\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">GeometryField.geography</span></code></a> 关键字，以便使用 <a class=\"reference internal\" href=\"#geography-type\"><span class=\"std std-ref\">地理数据库类型</span></a>。</p>\n</aside>\n<p>其他资源：</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference external\" href=\"https://spatialreference.org/\">spatialreference.org</a>: A database of spatial reference systems.</p></li>\n<li><p><a class=\"reference external\" href=\"https://web.archive.org/web/20080302095452/http://welcome.warnercnr.colostate.edu/class_info/nr502/lg3/datums_coordinates/spcs.html\">The State Plane Coordinate System</a>: A website covering the various\nprojection systems used in the United States. Much of the U.S. spatial\ndata encountered will be in one of these coordinate systems rather than\nin a geographic coordinate system such as WGS84.</p></li>\n</ul>\n</section>\n</section>\n<section id=\"spatial-index\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">spatial_index</span></code><a class=\"heading-anchor\" href=\"#spatial-index\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.BaseSpatialField.spatial_index\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseSpatialField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">spatial_index</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.BaseSpatialField.spatial_index\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Defaults to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>. Creates a spatial index for the given geometry\nfield.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>This is different from the <code class=\"docutils literal notranslate\"><span class=\"pre\">db_index</span></code> field option because spatial\nindexes are created in a different manner than regular database\nindexes. Specifically, spatial indexes are typically created using\na variant of the R-Tree, while regular database indexes typically\nuse B-Trees.</p>\n</aside>\n</section>\n</section>\n<section id=\"geometry-field-options\">\n<span id=\"id15\"></span><h2>几何字段选项<a class=\"heading-anchor\" href=\"#geometry-field-options\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>几何字段还有其他可用的选项。以下所有选项都是可选的。</p>\n<section id=\"dim\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">dim</span></code><a class=\"heading-anchor\" href=\"#dim\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.GeometryField.dim\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">GeometryField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">dim</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.GeometryField.dim\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>This option may be used for customizing the coordinate dimension of the\ngeometry field. By default, it is set to 2, for representing two-dimensional\ngeometries. For spatial backends that support it, it may be set to 3 for\nthree-dimensional support.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>目前，对三维的支持仅限于 PostGIS 和 SpatiaLite 后端。</p>\n</aside>\n</section>\n<section id=\"geography\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">geography</span></code><a class=\"heading-anchor\" href=\"#geography\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.GeometryField.geography\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">GeometryField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">geography</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.GeometryField.geography\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>If set to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, this option will create a database column of\ntype geography, rather than geometry. Please refer to the\n<a class=\"reference internal\" href=\"#geography-type\"><span class=\"std std-ref\">geography type</span></a> section below for more\ndetails.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>地理支持仅限于 PostGIS，并且会强制将 SRID 设置为 4326。</p>\n</aside>\n</section>\n<section id=\"max-geom-collections\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">max_geom_collections</span></code><a class=\"heading-anchor\" href=\"#max-geom-collections\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.gis.db.models.GeometryField.max_geom_collections\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">GeometryField.</span></span><span class=\"sig-name descname\"><span class=\"pre\">max_geom_collections</span></span><a class=\"heading-anchor\" href=\"#django.contrib.gis.db.models.GeometryField.max_geom_collections\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<aside class=\"version-note version-added\" data-version=\"5.2.17\">\n<p class=\"version-note-title\">New in Django 5.2.17</p></aside>\n<p>This option is forwarded to the <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/forms-api/#django.contrib.gis.forms.Field.max_geom_collections\" title=\"django.contrib.gis.forms.Field.max_geom_collections\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">form</span> <span class=\"pre\">field</span></code></a> generated for this model\nfield, bounding how many geometry collections may be contained in submitted\nWKB/WKT inputs before raising <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#ValueError\" title=\"(in Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">ValueError</span></code></a>. Since spatial field\nassignments are lazy, it is also checked when values are accessed, e.g. when\nsaving an instance, but not when read from a database. It defaults to <code class=\"docutils literal notranslate\"><span class=\"pre\">198</span></code>.</p>\n<section id=\"geography-type\">\n<span id=\"id16\"></span><h4>地理类型<a class=\"heading-anchor\" href=\"#geography-type\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The geography type provides native support for spatial features represented\nwith geographic coordinates (e.g., WGS84 longitude/latitude). <a class=\"footnote-reference brackets\" href=\"#fngeography\" id=\"id17\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>6<span class=\"fn-bracket\">]</span></a>\nUnlike the plane used by a geometry type, the geography type uses a spherical\nrepresentation of its data. Distance and measurement operations\nperformed on a geography column automatically employ great circle arc\ncalculations and return linear units. In other words, when <code class=\"docutils literal notranslate\"><span class=\"pre\">ST_Distance</span></code>\nis called on two geographies, a value in meters is returned (as opposed\nto degrees if called on a geometry column in WGS84).</p>\n<p>由于地理计算涉及更多的数学运算，因此地理类型仅支持 PostGIS 空间查询的子集。实际上，这意味着除了 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geoquerysets/#distance-lookups\"><span class=\"std std-ref\">距离查询</span></a> 之外，地理列仅支持以下其他 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geoquerysets/#spatial-lookups\"><span class=\"std std-ref\">空间查询</span></a>：</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geoquerysets/#std-fieldlookup-bboverlaps\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">bboverlaps</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geoquerysets/#std-fieldlookup-coveredby\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">coveredby</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geoquerysets/#std-fieldlookup-covers\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">covers</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/gis/geoquerysets/#std-fieldlookup-intersects\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">intersects</span></code></a></p></li>\n</ul>\n<p>如果您需要使用不支持地理类型作为输入的空间查询或聚合函数，您可以在查询中使用 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/models/database-functions/#django.db.models.functions.Cast\" title=\"django.db.models.functions.Cast\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Cast</span></code></a> 数据库函数将地理列转换为几何类型：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.gis.db.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">PointField</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db.models.functions</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Cast</span>\n\n<span class=\"n\">Zipcode</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">annotate</span><span class=\"p\">(</span><span class=\"n\">geom</span><span class=\"o\">=</span><span class=\"n\">Cast</span><span class=\"p\">(</span><span class=\"s2\">&quot;geography_field&quot;</span><span class=\"p\">,</span> <span class=\"n\">PointField</span><span class=\"p\">()))</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span>\n    <span class=\"n\">geom__within</span><span class=\"o\">=</span><span class=\"n\">poly</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>For more information, the PostGIS documentation contains a helpful section on\ndetermining <a class=\"reference external\" href=\"https://postgis.net/docs/using_postgis_dbmanagement.html#PostGIS_GeographyVSGeometry\">when to use geography data type over geometry data type</a>.</p>\n<p class=\"rubric\">脚注</p>\n<aside class=\"footnote-list brackets\">\n<aside class=\"footnote brackets\" id=\"fnogc\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id3\">1</a><span class=\"fn-bracket\">]</span></span>\n<p>OpenGIS Consortium, Inc., <a class=\"reference external\" href=\"https://www.ogc.org/standard/sfs/\">Simple Feature Specification For SQL</a>.</p>\n</aside>\n<aside class=\"footnote brackets\" id=\"fnogcsrid\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id4\">2</a><span class=\"fn-bracket\">]</span></span>\n<p><em>在第 2.3.8 章，第 39页 查看 id</em> （几何值和空间参考系统）。</p>\n</aside>\n<aside class=\"footnote brackets\" id=\"fnsrid\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id7\">3</a><span class=\"fn-bracket\">]</span></span>\n<p>Typically, SRID integer corresponds to an EPSG (<a class=\"reference external\" href=\"https://epsg.org/\">European Petroleum Survey Group</a>) identifier. However, it may also be associated with custom projections defined in spatial database's spatial reference systems table.</p>\n</aside>\n<aside class=\"footnote brackets\" id=\"fnthematic\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id8\">4</a><span class=\"fn-bracket\">]</span></span>\n<p>Terry A. Slocum, Robert B. McMaster, Fritz C. Kessler 和 Hugh H. Howard，《主题制图和地理可视化》（Prentice Hall，第二版），第 7.1.3 节。</p>\n</aside>\n<aside class=\"footnote brackets\" id=\"fndist\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id9\">5</a><span class=\"fn-bracket\">]</span></span>\n<p>这个限制不适用于 PostGIS。</p>\n</aside>\n<aside class=\"footnote brackets\" id=\"fngeography\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id17\">6</a><span class=\"fn-bracket\">]</span></span>\n<p>请参考 <a class=\"reference external\" href=\"https://postgis.net/docs/using_postgis_dbmanagement.html#PostGIS_Geography\">PostGIS Geography Type</a> 文档以获取更多详细信息。</p>\n</aside>\n</aside>\n</section>\n</section>\n</section>","rootId":"module-django.contrib.gis.db.models","toc":[{"title":"空间字段类型","anchor":"spatial-field-types","children":[{"title":"GeometryField","anchor":"geometryfield","children":[]},{"title":"PointField","anchor":"pointfield","children":[]},{"title":"LineStringField","anchor":"linestringfield","children":[]},{"title":"PolygonField","anchor":"polygonfield","children":[]},{"title":"MultiPointField","anchor":"multipointfield","children":[]},{"title":"MultiLineStringField","anchor":"multilinestringfield","children":[]},{"title":"MultiPolygonField","anchor":"multipolygonfield","children":[]},{"title":"GeometryCollectionField","anchor":"geometrycollectionfield","children":[]},{"title":"RasterField","anchor":"rasterfield","children":[]}]},{"title":"空间字段选项","anchor":"spatial-field-options","children":[{"title":"srid","anchor":"srid","children":[{"title":"选择一个 SRID","anchor":"selecting-an-srid","children":[]}]},{"title":"spatial_index","anchor":"spatial-index","children":[]}]},{"title":"几何字段选项","anchor":"geometry-field-options","children":[{"title":"dim","anchor":"dim","children":[]},{"title":"geography","anchor":"geography","children":[]},{"title":"max_geom_collections","anchor":"max-geom-collections","children":[{"title":"地理类型","anchor":"geography-type","children":[]}]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API 参考","url":"/zh-hans/6.1/ref/"},{"docname":"ref/contrib/index","title":"contrib 包","url":"/zh-hans/6.1/ref/contrib/"},{"docname":"ref/contrib/gis/index","title":"GeoDjango","url":"/zh-hans/6.1/ref/contrib/gis/"}],"prev":{"docname":"ref/contrib/gis/install/spatialite","title":"安装 SpatiaLite","url":"/zh-hans/6.1/ref/contrib/gis/install/spatialite/"},"next":{"docname":"ref/contrib/gis/db-api","title":"GeoDjango 数据库 API","url":"/zh-hans/6.1/ref/contrib/gis/db-api/"},"formats":{"html":"/zh-hans/6.1/ref/contrib/gis/model-api/","markdown":"/zh-hans/6.1/ref/contrib/gis/model-api.md","json":"/zh-hans/6.1/ref/contrib/gis/model-api.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/ref/contrib/gis/model-api.txt","official":"https://docs.djangoproject.com/zh-hans/6.1/ref/contrib/gis/model-api/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}