[Django]-Is a GeoDjango MultiPolygonField supposed to accept a Polygon geometry?

3👍

EDIT:

Although the MultiPolygon code seems to allow a Polygon to b stored as MultiPolygon object:

class MultiPolygon(GeometryCollection):
    _allowed = Polygon
    _typeid = 6

the issue you are presenting rises as you describe it.

I have tried some workarounds and the only one that I am somewhat satisfied with is to refactor your geom field into a generic GeometryField that can store any type of geometry.

Another option without touching your model would be to convert each Polygon to a MultiPolygon before inserting it to the field:

p = Polygon()
location.mpoly = MultiPolygon(p)

Seems to me that this is worthy of an issue with either a Tutorial update request, or a code fix.


Leaving the previous state of the answer here for comment continuity:

The issue is with the geography=True setting and not with the field, because the MultiPolygonField does accept a Polygon as well as a MultiPolygon:

The geography type provides native support for spatial features represented with geographic coordinates (e.g., WGS84 longitude/latitude). Unlike the plane used by a geometry type, the geography type uses a spherical representation of its data. Distance and measurement operations performed on a geography column automatically employ great circle arc calculations and return linear units. In other words, when ST_Distance is called on two geographies, a value in meters is returned (as opposed to degrees if called on a geometry column in WGS84).

Since you set the field to expect a geography type object, then if you try to pass a non-geography representation of a Polygon, you will get the error in question.

Leave a comment