drf-extra-fields icon indicating copy to clipboard operation
drf-extra-fields copied to clipboard

invalid GEOS Geometry index: 0 Error occurs on nullable `PointField`

Open mohammadhasanzadeh opened this issue 3 years ago • 3 comments

The following error occurs if we have a nullable PointField on the model: IndexError: invalid GEOS Geometry index: 0

Model field: checkout_position = models.PointField(geography=True, null=True, blank=True)

Serializer field: checkout_position = PointField(required=False)

mohammadhasanzadeh avatar Jan 20 '21 13:01 mohammadhasanzadeh

Could you please provide the request data? Seems to me that checkout_position received a data other than None, '', [], (), {}.

alicertel avatar Feb 03 '21 14:02 alicertel

due to checkout_position is not required field, so we never send any value like the None, '', [], (), {} in the request if we do not need to the checkout_position data, but if we need to the checkout_position data, we send data like the following:

{
    "id": 64,
    "checkout_time": "2021-02-05T11:13:27.736",
    "checkout_accuracy": 15.62,
    "checkout_position": {
        "longitude": 51.398247,
        "latitude": 35.7225148
    }
}

mohammadhasanzadeh avatar Feb 05 '21 07:02 mohammadhasanzadeh

Hi @mohammadhasanzadeh,

When I test PointField with these serializer and data:

class PointSerializer(serializers.Serializer):
    checkout_position = PointField(required=False)

data = {
    "checkout_position": {
        "longitude": 51.398247,
        "latitude": 35.7225148
    }
}

results are fine:

In [17]: serializer = PointSerializer(data=data)

In [18]: serializer.is_valid()
Out[18]: True

In [19]: serializer.validated_data
Out[19]: OrderedDict([('checkout_position', <Point object at 0x7fd6b859eb10>)])

In [21]: str(serializer.validated_data["checkout_position"])
Out[21]: 'POINT (51.398247 35.7225148)'

Can you elaborate your problem more with the data you get the error?

KaratasFurkan avatar Feb 18 '21 16:02 KaratasFurkan