Search code examples
python-3.xdjangopostgis

PostGIS geography does not support the "~=" function/operator


I am trying to save point field in the database via update_or_create method but it is giving me this error.

My function to save data:

    for city in cities_data:
        obj, created = models.City.objects.update_or_create(
            name = city["name"],
            country = city["country"],
            state = city["state"],
            point = Point(
                float(city["longitude"]),
                float(city["latitude"])   
            ),
            radius = city["radius"],
            is_curated = city["is_curated"],
            is_metro_city = city["is_metro_city"]
        )
        obj.save()

Model:

class City(models.Model):
    name = models.CharField(max_length=50)
    country = models.CharField(max_length=50)
    state = models.CharField(max_length=50)
    point = gis_models.PointField(geography=True, null=True)
    is_metro_city = models.BooleanField(default=False)
    is_curated = models.BooleanField(default=False)
    radius = models.IntegerField(null=True)

when I try to run this I get this error:

ValueError: PostGIS geography does not support the "~=" function/operator.

I want to know why I am getting this error, I did not find any useful information related to this. Thanks in Advance.


Solution

  • If you want to use this you can use it like this:

            obj, created = models.City.objects.update_or_create(
                **city_data,
                defaults={'point': Point(
                    float(city["longitude"]),
                    float(city["latitude"])   
                )}
            )
    

    I do not know why it does not work that way, but I made it work this way, if you find it out why it does not work you can improve the answer.